# Fractals/Mathematics/Vector field

Vector field
Here mainly numerical methods for time independent 2D vector fields are described.

# Dictionary

* vector function is a function that gives vector as an output
* field : space (plane, sphere, ... )
* field line is a line that is everywhere tangent to a given vector field
* scalar/ vector / tensor:
** Scalars are real numbers used in linear algebra. Scalar is a tensor of zero order
** Vector is a tensor of first order. Vector is an extension of scalar
** tensor is an extension of vector

## Vector

Forms of 2D vector:
* [z1] ( only one complex number when first point is known , for example z0 is origin
* [z0, z1] = two complex numbers
* 4 scalars ( real numbers)
** [x, y, dx , dy]
** [x0, y0, x1, y1]
** [x, y, angle, magnitude]
* 2 scalars : [x1, y1] for second complex number when first point is known , for example z0 is origin

## gradient

The numerical gradient of a function:
* "is a way to estimate the values of the partial derivatives in each dimension using the known values of the function at certain points."
Gradient function G of function f at point (x0,y0)
Input
* function f
* point (x0,y0) where gradient is computed
Output:
* vector from (x0,yo) to (x1,y1) = gradient
See:
* Numerical differentiation in wikipedia
Computation:
* "The gradient is calculated as: (f(x + h) - f(x - h)) / (2*h) where h is a small number, typically 1e-5 f(x) will be called for each input elements with +h and -h pertubation. For gradient checking, recommend using float64 types to assure numerical precision."
* in matlab
* in R
* python

## equation
# Field types

Criteria for classification
* Autonomous / Non-autonomous
* time independent (= stationary = Steady ) or time dependent ( unsteady flow)
* dimension: 2D, 3D, ...
* mesh ( grid) type
* scalar function
** potential
** Force type: electric, magnetic, ...
* vector function
** equation ( for symbolic computations) - ODE
** numerical values ( for numerical computations)
*** field line integration method (scheme):
Force types A gravitational force fields
* the field lines are the solution of $g'=F$
* the trajectory of a test mass is the solution of $mg''=F$
where
* g is the standard gravity
* m is a mass
* F is a force field
An electric field
* The field lines are the paths that a point positive charge would follow as it is forced to move within the field. Field lines due to stationary charges have several important properties, including always originating from positive charges and terminating at negative charges, they enter all good conductors at right angles, and they never cross or close in on themselves.  The field lines are a representative concept; the field actually permeates all the intervening space between the lines. More or fewer lines may be drawn depending on the precision to which it is desired to represent the field. The study of electric fields created by stationary charges is called electrostatics.
* Diagram of electric charges induced in conductive objects (shapes) by the electrostatic field (lines with arrows) of a nearby charge (+), due to electrostatic induction
* Electric field around two identical conducting spheres at opposite electric potential

## classification

* Adrien Douady, Franiso Estrada, and Pierrette Sentena. Champs de veteurs polynomiaux sur C. Unpublished manuscript
* Classification of Complex Polynomial Vector Fields in One Complex Variable by Bodil Branner, Kealey Dias
* On Parameter Space of Complex Polynomial Vector Fields in the Complex Plane by Kealey Dias, Lei Tan

# Algorithm

* start with
** plane (parameter plane or dynamic plane)
** scalar function
** vector function
* create scalar field using scalar function ( potential)
* create vector field from scalar field using vector function ( gradient of the potential)
* compute:
** filed lines ( stream lines )
*** External Rays on the Parameter Plane
*** dynamic external rays
** contour lines ( equipotential lines )
** map whole field using
*** Line Integral Convolution (LIC)

## separatrix

The separatrix is clearly visible by numerically solving for trajectories backwards in time. Since when solving for the trajectories forwards in time, trajectories diverge from the separatrix, when solving backwards in time, trajectories converge to the separatrix.

## gradient descent

* Gradient Descent by Aytan Hajiyeva Aytan Hajiyeva Oct 1, 2021
* Gradient Descent algorithm by Raghunath D Raghunath D Jan 28, 2019
* An overview of gradient descent optimization algorithms by SEBASTIAN RUDER 19 JAN 2016 and pdf from arxiv
* Escaping from Saddle Points by Rong Ge • Mar 22, 2016
* An Easy Guide to Gradient Descent in Machine Learning By Great Learning Team -May 24, 2020
* Gradient Descent in Python Sagar Mainkar Sagar Mainkar Aug 25, 2018

## Field line computing

Problem Statement:
* Field line tracing ( not curve sketching}
* drawing contour maps ( in computer graphic) = Numerical continuation ( in math)
* compute an integral curve from a seed point through a vector field without any analysis of its structure on the uniform grid ( raster scan or pixels)
* Natural parameter continuation, a very simple adaptation of the iterative solver to a parametrized problem
* Simplicial linear continuation algorithm.
* Pseudo-arclength continuation, based on the observation that the "ideal" parameterization of a curve is arclength, is an approximation of the arclength in the tangent space of the curve\
Methods ( solvers) available for the field-lines
* Euler
* RK2
* RK3
* RK4 - the original authors of those sampling algorithms: Runge und Kutta.
* How do vector field Pathfinding algorithm work ? by PDN - PasDeNom

#### Construction

Given a vector field $\mathbf {F} (\mathbf {x} )$ and a starting point $\mathbf {x} _{\text{0}}$ a field line can be constructed iteratively by finding the field vector at that point $\mathbf {F} (\mathbf {x} _{\text{0}})$. The unit tangent vector at that point is: $\mathbf {F} (\mathbf {x} _{\text{0}})/|\mathbf {F} (\mathbf {x} _{\text{0}})|$. By moving a short distance $ds$ along the field direction a new point on the line can be found
    $\mathbf {x} _{\text{1}}=\mathbf {x} _{\text{0}}+{\mathbf {F} (\mathbf {x} _{\text{0}}) \over |\mathbf {F} (\mathbf {x} _{\text{0}})|}ds$
Then the field at that point $\mathbf {F} (\mathbf {x} _{\text{1}})$ is found and moving a further distance $ds$ in that direction the next point of the field line is found
    $\mathbf {x} _{\text{2}}=\mathbf {x} _{\text{1}}+{\mathbf {F} (\mathbf {x} _{\text{1}}) \over |\mathbf {F} (\mathbf {x} _{\text{1}})|}ds$
By repeating this and connecting the points,the field line can be extended as far as desired. This is only an approximation to the actual field line, since each straight segment isn't actually tangent to the field along its length, just at its starting point. But by using a small enough value for $ds$, taking a greater number of shorter steps, the field line can be approximated as closely as desired. The field line can be extended in the opposite direction from $\mathbf {x} _{\text{0}}$ by taking each step in the opposite direction by using a negative step $-ds$.

### rk4 numerical integration method

Fourth-order Runge-Kutta (RK4) in case of 2D time independent vector field
$F$ is a vector function that for each point p
p = (x, y)
in a domain assigns a vector v
$v=F(p)=F(x,y)=(F_{1}(x,y),F_{2}(x,y))$
where each of the functions $F_{i}$ is a scalar function:
$F_{i}:\mathbb {R} ^{2}\to \mathbb {R}$
A field line is a line that is everywhere tangent to a given vector field.
Let r(s) be a field line given by a system of ordinary differential equations, which written on vector form is:
${\frac {dr}{ds}}=F(r(s))$
where:
* s representing the arc length along the field line, like for example continous iteration count
* $r(0)=r_{0}$ is a seed point

#### 2 variables

Given a seed point $r_{0}$ on the field line, the update rule ( RK4) to find the next point $r_{i}$along the field line is
$r_{i+1}=r_{i}+{\frac {h(k_{1}+2k_{2}+2k_{3}+k_{4})}{6}}$
where:
* h is the step size along field line = ds
* k are the intermediate vectors:
${\begin{array}{lcl}k_{1}=F(r_{i})\\k_{2}=F(r_{i}+{\frac {h}{2}}k_{1})\\k_{3}=F(r_{i}+{\frac {h}{2}}k_{2})\\k_{4}=F(r_{i}+hk_{3})\end{array}}$

#### only x

Here
Given a seed point $r_{0}$ on the field line, the update rule ( RK4) to find the next point $r_{i}$along the field line is
where:
* h is the step size along field line = dx
* k are the intermediate vectors:
${\begin{array}{lcl}k_{1}=F(x_{i})\\k_{2}=F(x_{i}+{\frac {h}{2}}k_{1})\\k_{3}=F(x_{i}+{\frac {h}{2}}k_{2})\\k_{4}=F(x_{i}+hk_{3})\end{array}}$
Examples:
* ${\frac {dy}{dx}}=x$
* $y^{'}+x^{2}+x=0$
* ${\frac {dy}{dx}}=x^{2}-x-2$
* Slope field (black), some solutions (red) and isoclines (blue) of y'=x
* Three integral curves for the slope field corresponding to the differential equation dy / dx = x$^2$ − x − 2

## Visualisation of vector field

Plot types (Visualization Techniques for Flow Data) :
* Glyphs = Icons or signs for visualizing vector fields
** simplest glyph = Line segment (hedgehog plots)
** arrow plot = quiver plot = Hedgehogs (global arrow plots)
* Characteristic Lines
** streamlines = curve everywhere tangential to the instantaneous vector (velocity) field (time independent vector field). For time independent vector field streaklines = Path lines = streak lines
* texture (line integral convolution = LIC)
* Topological skeleton
** fixed point extraction ( Jacobian)
* LIC
* Streamlines and streamtube
* Image-based flow visualization
* Integral Curves
* integral curves

### quiver plot

Definition
* "A quiver plot displays velocity vectors as arrows with components (u,v) at the points (x,y)"

### stream plot

* A stream plot uses stream lines
* wolfram: ComplexStreamPlot

# Example fields
### flowmaps

* FlowMap Painter by TECK LEE TAN
* Flowmaps, gradient maps, gas giants by Martin Donald
* godot game engine

## fBM

fBM stands for Fractional Brownian Motion
* i quilezles : fbmsdf
* i quilezles: fbm

## SDF

SDF = Signed Distance Function
* by dimension ( 1D, 2D, 3D, ...)
* by color
* by distance function ( Euclid distance,
* algorithm
** the efficient fast marching method,
*** ray marching
*** like Marching Parabolas, a linear-time CPU-amenable algorithm.
** Min Erosion, a simple-to-implement GPU-amenable algorithm
** fast sweeping method
** the more general level-set method.
* visualisation ( gray gradient, LSM,
* simple predefined figures or arbitrary shape
It is not
* Sqlce Database File (.SDF) is a database created and accessed by SQL Server Compact Edition. = sdf tag in SO
* SDFormat (Simulation Description Format), sometimes abbreviated as SDF, is an XML format that describes objects and environments for robot simulators, visualization, and control.
* ScientificDataFormat (SDF) and SDF python package = to read, write and interpolate multi-dimensional data. The Scientific Data Format is an open file format based on HDF5 to store multi-dimensional data such as parameters, simulation results or measurements.
* SuiteCloud Development Framework (SDF)

### single channel color

* SDF for 2d primitives
* normals to SDF
* gradient
* i quilezles : interior distance ( SDF)
* Glyphs, shapes, fonts, signed distance fields by Martin Donald
* CSC2547 DeepSDF Learning Continuous Signed Distance Functions for Shape Representation
* 2D Signed Distance Field Basics by Ronja
* snelly is a WebGL SDF pathtracer by Jamie Portsmouth
* shadertoy : SDF Raymarch Quadtree by paniq
* WGSL 2D SDF Primitives by munrocket
* Define 3 shapes via there Signed Distance Function
* Signed_distance_function in wikipedia
* dist functions 2d by I Quilez
* i quilez : dist grad functions 2d
* SDF
* 8-points Signed Sequential Euclidean Distance Transform
* Distance Transforms of Sampled Functions
** Linear Time Distance Transform (dtform)
** Distance Transforms of Sampled Functions in C++
** Distance Fields
* heman

### multichannel

* Chlumsky: msdfgen =Multi-channel signed distance field generator
* image-sdf = Command-line tool which takes a 4-channel RGBA image and generates a signed distance field. The bitmask is determined by pixels with alpha over 128 and any RGB channel over 128.

### mesh

* fogleman sdf Simple SDF mesh generation in Python
* christopher batty SDFGen A simple commandline utility to generate grid-based signed distance field (level set) generator from triangle meshes,

### Adaptively Sampled Distance Fields

* Adaptively Sampled Distance Fields (ADFs) Representing Shape for Computer Graphics

### deep

* signed distance fields for arbitrary meshes

### fonts, glyphs

* Glyphs, shapes, fonts, signed distance fields

### circle

* Disk - distance 2D by iq -good code
* Shader Tutorial | Intro to Signed Distance Fields by Suboptimal Engineer code is not working in actual shadertoy
* c code
* WGLS

### hg_sdf

hg_sdf: A glsl library for building signed distance functions
* mercury -
* ink4
* NVScene 2015 Session: How to Create Content with Signed Distance Functions (Johann Korndörfer)
* alan zucconi: signed-distance-functions
* jcowles - A WebGL friendly port of Mercury's hg_sdf library

### blender

* b3dsdf = A toolkit of 2D/3D distance functions, sdf/vector ops and various utility shader nodegroups (159+) for Blender 2.83+.

## Potential of Mandelbrot set

* i quilezles: distance to fractals
* Parameter External Rays are field lines of potential field
* ( Empty) field : points of rectangular mesh
* Scalar field : potential of Mandelbrot set
* Vector field: Array plot of the gradient

# Programs

* First order, autonomous systems of ODEs
* Runge-Kutta for systems of ODEs
* The geometry and numerics of first order ODEs
* flow-lines by MAKS SURGUY
* symbolab : ordinary-differential-equation-calculator
* PETSc - the Portable, Extensible Toolkit for Scientific Computation, pronounced PET-see (/ˈpɛt-siː/), is for the scalable (parallel) solution of scientific applications modeled by partial differential equations. It has bindings for C, Fortran, and Python (via petsc4py). PETSc also contains TAO, the Toolkit for Advanced Optimization, software library. It supports MPI, and GPUs through CUDA, HIP or OpenCL, as well as hybrid MPI-GPU parallelism; it also supports the NEC-SX Tsubasa Vector Engine.
* Maxima CAS
* CGAL - 2D_Placement_of_Streamlines by Abdelkrim Mebarki
* Python
** flowtopolopy : A Python package to perform topology and topology-based analyses of flow fields
** vector-field-visualizer
** Matplotlib (python library ):
*** quiver
*** stream plot
*** quiver-plot-with-matplotlib-and-jupyter-notebooks
*** short scripts for making 2D vector plots for visualizing an equation by Benjamin Badger
** VectorFieldPlot , images created with VectorFieldPlot
* OpenProcessing
** Flow field #2 by Naoki Tsutae
* G'MIC - display_quiver
* OpenCV
** dsp.stackexchange: how-to-detect-gradients-in-images - Histogram of Oriented Gradients ( HOG )
** arrowed line
* Maxima CAS
** plotdf ( for ODE)
* C++
** Visualizing 2D Vector Fields by Philip Rideout, 18 July 2018 and clumpy
* par_streamlines ( C, WebGl) by Philip Allan Rideout, github: prideout/par
* JavaScript
** Streamlines calculator by anvaka (Andrei Kashcha)
* c
** vfplot - program for plotting two-dimensional vector fields by J J Green
** rk4, a C code which implements a fourth-order Runge-Kutta method to solve an ordinary differential equation (ODE). by j burkardt
** gsl
* js
** visualize-2d vector-fields-with-three-js and

# Examples
## Images

* images from commons : Category:Field_lines
* images from commons : Category:Vector_fields
* fractalforums.org : cruising-through-fractal-flow-fields

## Shadertoy

* tag: vector field
* arrows = quiver plot
** 2D Vector Field Flow by morgan3d
** static 2d by celeriac
* Line Integral Convolution (LIC)
** Flow Field LIC Created by Thomas_Diewald in 2017-10-10
** LIC 2D / flow 2D - precomp+lines by FabriceNeyret2 in 2017-03-04
* 3D vector field

## Videos

* by Chris Thomasson
** Test Animated Vector Field "Power Flux", try 0...
** Test Animated Vector Field "Power Flux", try 1...
* Numerical Approximations of Gradients from Deeplearning.ai
* Sticking to the pointy ends: electric field lines in an evolving Julia set capacitor by Nils Berglund

### rboyce1000

* Bifurcation of Quartic Polynomial Vector Fields by @rboyce1000
** 1/3
** 2/3
** 3/3
Algorithm
* create polynomial with desired properities
** f(z) = z*g(z) with root at origin
** g(z) is a 3-rd root of unity = $z^{3}-1$
f(z) = z(z^3 - 1)
One can check it with Maxima CAS
to rotate it around origin let's change 1 with :$\quad e^{2\pi it}$ ( multiplier of the fixed point) where t is a proper fraction in turns
    $f_{t}(z)=z(z^{3}-e^{2\pi it})$
Original function from comments:
    $f_{t}(z)=z(z^{3}-1.5^{3}e^{2\pi it})$

# See also

* Vector flow in wikipedia
* TYLER HOBBS : flow-fields

# References

* ↑ Vector field in wikipedia
* ↑ Euclidian vector in wikipedia
* ↑ matlab : gradient function
* ↑ stackoverflow question: is-there-any-standard-way-to-calculate-the-numerical-gradient
* ↑ nnp_numerical_gradient by Mamy Ratsimbazafy
* ↑ matrixlab-examples : gradient
* ↑ matlab-function-gradient-numerical-gradient by itectec
* ↑ numDeriv : grad
* ↑ numpy : gradient
* ↑ Numerical Methods for Particle Tracing in Vector Fields by Kenneth I. Joy
* ↑ curve sketching by David Guichard and friends
* ↑ liruics : Introduction to Scientific Visualization - Flow Field
* ↑ raster-algorithms-basic-computer-graphics-part-2 by what-when-how
* ↑ bolster.academy : Euler's method interactive
* ↑ interactive Runge Kutta 4 by Greg Petrics
* ↑ grasshopper3d forum: field-lines-how-to-rebuild-and-make-periodic?overrideMobileRedirect=1
* ↑ Classification and visualisation of critical points in 3d vector fields. Master thesis by Furuheim and Aasen
* ↑ Classification and visualisation of critical points in 3d vector fields. Master thesis by Furuheim and Aasen
* ↑ Weisstein, Eric W. "Integral Curve." From MathWorld--A Wolfram Web Resource
* ↑ Flow Visualisation from TUV
* ↑ Data visualisation by Tomáš Fabián
* ↑ A Streakline Representation of Flow in Crowded Scenes from UCF
* ↑ lic by Zhanping Liu
* ↑ Vector Field Topology in Flow Analysis and Visualization by Guoning Chen
* ↑ Vector Field Visualization by Leif Kobbelt
* ↑ matlab ref : quiver plot
* ↑ CedricGuillemet: SDF = Collection of resources (papers, links, discussions, shadertoys,...) related to Signed Distance Field
* ↑ Distance Fields by Philip Rideout in 2018
* ↑ ray-march = Rendering procedural 3D geometry by raymarching through a distance field
* How I built a wind map with WebGL by Vladimir Agafonkin
* math.stackexchange question : what-do-polynomials-look-like-in-the-complex-plane
* mathematica.stackexchange question : visualizing-a-complex-vector-field-near-poles
* fractalforums.com : smooth-external-angle-of-mandelbrot-set
* J.M. Hyman, M. Shashkov, Natural discretizations for the divergence, gradient, and curl on logically rectangular grids, Computers & Mathematics with Applications, Volume 33, Issue 4, 1997, Pages 81-104, ISSN 0898-1221