public void ComputeStatistics(out float validRegion, out float mean, out float sd) { int numValidCells = 0; mean = 0; sd = 0; GridIndex range = new GridIndex(Size); foreach (GridIndex idx in range) { float s = this[(int)idx]; if (s != InvalidValue) { numValidCells++; mean += s; } } validRegion = (float)numValidCells / Size.Product(); mean /= numValidCells; // Compute standard derivative. range.Reset(); foreach (GridIndex idx in range) { float s = this[(int)idx]; if (s != InvalidValue) { float diff = s - mean; sd += diff * diff; } } sd /= numValidCells; sd = (float)Math.Sqrt(sd); }
public VectorFieldUnsteady(VectorFieldUnsteady field, VFJFunction function, int outputDim) { int scalars = outputDim; FieldGrid gridCopy = field._scalarsUnsteady[0].TimeSlices[0].Grid.Copy(); _scalarsUnsteady = new ScalarFieldUnsteady[outputDim]; // Reserve the space. for (int comp = 0; comp < outputDim; ++comp) { ScalarField[] fields = new ScalarField[field.Size.T]; //(field.Grid); for (int t = 0; t < field.Size.T; ++t) { fields[t] = new ScalarField(gridCopy); } _scalarsUnsteady[comp] = new ScalarFieldUnsteady(fields); _scalarsUnsteady[comp].TimeOrigin = field[0].TimeOrigin ?? 0; _scalarsUnsteady[comp].InvalidValue = field.InvalidValue; _scalarsUnsteady[comp].DoNotScale(); } this.InvalidValue = field.InvalidValue; this.TimeOrigin = field.TimeOrigin; Grid = field.Grid.Copy(); // Since the time component is in the grid size as well, we do not need to account for time specially. GridIndex indexIterator = new GridIndex(field.Size); foreach (GridIndex index in indexIterator) { Vector v = field.Sample((int)index); if (v[0] == InvalidValue) { for (int dim = 0; dim < Scalars.Length; ++dim) { _scalarsUnsteady[dim][(int)index] = (float)InvalidValue; } continue; } SquareMatrix J = field.SampleDerivative(index); Vector funcValue = function(v, J); for (int dim = 0; dim < Scalars.Length; ++dim) { Scalars[dim][(int)index] = funcValue[dim]; } } }
public VectorField(VectorField field, VFJFunction function, int outputDim, bool needJacobian = true) { int scalars = outputDim;/*function(field.Sample(0), field.SampleDerivative(new Vector(0, field.Size.Length))).Length;*/ _scalars = new ScalarField[scalars]; FieldGrid gridCopy = field.Grid.Copy(); // In case the input field was time dependant, this one is not. Still, we keep the size and origin of the time as new dimension! gridCopy.TimeDependant = false; for (int dim = 0; dim < scalars; ++dim) { Scalars[dim] = new ScalarField(gridCopy); } this.InvalidValue = field.InvalidValue; GridIndex indexIterator = new GridIndex(field.Size); foreach (GridIndex index in indexIterator) { Vector v = field.Sample((int)index); if (v[0] == InvalidValue) { for (int dim = 0; dim < Scalars.Length; ++dim) { Scalars[dim][(int)index] = (float)InvalidValue; } continue; } SquareMatrix J = needJacobian? field.SampleDerivative(index) : null; Vector funcValue = function(v, J); for (int dim = 0; dim < Scalars.Length; ++dim) { var vec = Scalars[dim]; Scalars[dim][(int)index] = funcValue[dim]; } } }
public ScalarField(ScalarField field, SGFunction function, bool needJ = true) { _data = new float[field.Size.Product()]; Grid = field.Grid; this.InvalidValue = field.InvalidValue; GridIndex indexIterator = new GridIndex(field.Size); foreach (GridIndex index in indexIterator) { float s = field[(int)index]; if (s == InvalidValue) { this[(int)index] = (float)InvalidValue; } else { Vector g = needJ ? field.SampleDerivative(index) : new Vec2(0, 0); this[(int)index] = function(s, g); } } }