//Sets new initial conditions and invalidates existing data public void SetState(VectorND newState, double currentTime) { InvalidateData(currentTime); int i = solution.Lookup(currentTime); solution.WriteState(solution.ChunkIndex(i), solution.LocalIndex(i), newState); }
public void ResetLabels(int[] trueValues) { if (trueValues == null) { trueValues = new int[0]; } if (trueValues.Any(o => o > 7)) { throw new ArgumentOutOfRangeException(""); } while (_viewport.Children.Count > _viewportOffset_Init) { _viewport.Children.RemoveAt(_viewport.Children.Count - 1); } VectorND center = new VectorND(.5, .5, .5); // Add the numbers for (int cntr = 0; cntr < 8; cntr++) { VectorND position = new VectorND(UtilityCore.ConvertToBase2(cntr, 3).Select(o => o ? 1d : 0d).ToArray()); position = position - center; position = center + (position * 1.2); Color color = trueValues.Contains(cntr) ? TrueColor : FalseColor; AddText3D(cntr.ToString(), position.ToPoint3D(), position.ToVector3D().ToUnit(), .1, color, false); } _viewportOffset_Labels = _viewport.Children.Count; }
private static void DrawSOMTile_Color(SelfOrganizingMapsWPF.DrawTileArgs e, VectorND source) { //NOTE: Copied from UtilityWPF.GetBitmap_RGB for (int y = 0; y < e.TileHeight; y++) { int offsetImageY = (e.ImageY + y) * e.Stride; int offsetSourceY = y * e.TileWidth * 3; for (int x = 0; x < e.TileWidth; x++) { int indexImage = offsetImageY + ((e.ImageX + x) * e.PixelWidth); int indexSource = offsetSourceY + (x * 3); byte r = (source[indexSource + 0] * 255).ToByte_Round(); byte g = (source[indexSource + 1] * 255).ToByte_Round(); byte b = (source[indexSource + 2] * 255).ToByte_Round(); e.BitmapPixelBytes[indexImage + 3] = 255; e.BitmapPixelBytes[indexImage + 2] = r; e.BitmapPixelBytes[indexImage + 1] = g; e.BitmapPixelBytes[indexImage + 0] = b; } } }
/// <summary> /// Setup a complete graph with n nodes. /// Positions are randomly distributed. /// </summary> public void SetupCompleteGraph(int n) { Nodes.Clear(); Edges.Clear(); GraphNode southPole = new GraphNode(new VectorND(new double[] { 0, 0, 0, -1 }), new VectorND(4)); southPole.FullLock(); Nodes.Add(southPole); System.Random rand = new System.Random(0); for (int i = 1; i < n; i++) { VectorND randPosition = new VectorND(new double[] { rand.NextDouble() - 0.5, rand.NextDouble() - 0.5, rand.NextDouble() - 0.5, rand.NextDouble() - 0.5 }); randPosition.Normalize(); Nodes.Add(new GraphNode(randPosition, new VectorND(4))); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { AddEdge(new GraphEdge(i, j)); } } }
private void UpdatePositionAndVelocity(GraphNode node, VectorND acceleration) { if (node.Lock) { return; } VectorND position = node.Position; VectorND velocity = node.Velocity; //if( position.IsOrigin ) // return; // Leapfrog method. double timeStep = 1; velocity += acceleration * timeStep; velocity *= .5; // Damping. position += velocity * timeStep; //position.Normalize(); position *= 5; //if( position.MagSquared > 1 ) //{ // position.Normalize(); // velocity = new VectorND( 3 ); //} node.Position = position; node.Velocity = velocity; //node.Acceleration = acceleration; Any reason to store this? }
public void checkConstructor() { VectorND v1 = new VectorND(1, 2); Assert.AreEqual(v1 [0], 1); Assert.AreEqual(v1 [1], 2); }
public static LatticeND ConvertStringToLatticeND(string str) { string adjustedLine; if (str.Contains(Environment.NewLine)) { adjustedLine = str.Remove(str.IndexOf(Environment.NewLine, StringComparison.Ordinal)); } else if (str.Contains(" ")) { adjustedLine = str.Replace(" ", ""); } else { adjustedLine = str; } if (adjustedLine.StartsWith(Languages.labelLatticeBasis)) { adjustedLine = adjustedLine.Substring(Languages.labelLatticeBasis.Length + 1); } if (!IsCharFigureOrSeparator(adjustedLine[1])) { adjustedLine = adjustedLine.Substring(1); adjustedLine = adjustedLine.Remove(adjustedLine.Length - 1); } int columns = adjustedLine.Count(t => t == adjustedLine[0]); adjustedLine = adjustedLine.Where(IsCharFigureOrSeparator).Aggregate("", (current, c) => current + c).Trim(' '); if (adjustedLine.Contains(";")) { adjustedLine = adjustedLine.Replace(';', ','); } else if (adjustedLine.Contains(" ")) { adjustedLine = adjustedLine.Replace(' ', ','); } else if (adjustedLine.Contains("\t")) { adjustedLine = adjustedLine.Replace('\t', ','); } string[] splittedLine = adjustedLine.Split(','); int rows = splittedLine.Length / columns; VectorND[] vectors = new VectorND[columns]; for (int i = 0; i < columns; i++) { vectors[i] = new VectorND(rows); for (int j = 0; j < rows; j++) { vectors[i].values[j] = BigInteger.Parse(splittedLine[rows * i + j]); } } return(new LatticeND(vectors, false)); }
public void IndexerTest() { VectorND v1 = new VectorND(4, -8, 0); Assert.AreEqual(v1[0], v1.values[0]); Assert.AreEqual(v1[1], v1.values[1]); Assert.AreEqual(v1[2], v1.values[2]); }
//Copy constructor public FunctionVectorND(VectorND tocopy) { dim = tocopy.GetDim(); if (dim > 0) { funcs = new FuncND[dim]; } }
private static SOMItem GetSOMItem_2D_Gray(VectorND item, ToVectorInstructions instr) { Convolution2D conv = new Convolution2D(item.VectorArray, instr.FromSizes[0], instr.FromSizes[1], false); conv = Convolute(conv, instr); return(new SOMItem(item, conv.Values.ToVectorND())); }
//Copy constructor public VectorND(VectorND tocopy) { dim = tocopy.GetDim(); if (dim > 0) { values = new float[dim]; } }
public static bool IsNearZero(this VectorND vector) { if (vector.VectorArray == null) { return(true); } return(vector.VectorArray.All(o => o.IsNearZero())); }
} //cluster it is assigned to public DataPoint(List <double> valuesi) { //all this code converts the List into an array and sets the index/initializes the data vector index = (int)valuesi[0]; valuesi.Remove(valuesi[0]); var values = valuesi.ToArray(); data = new VectorND(values, values.Length); cluster = 0; }
public void CopyConstructorTest() { VectorND v1 = new VectorND(4, -8, 0); VectorND v2 = new VectorND(v1); Assert.AreEqual(3, v2.GetDim()); Assert.AreEqual(v2[0], 4, required_accuracy); Assert.AreEqual(v2[1], -8, required_accuracy); Assert.AreEqual(v2[2], 0, required_accuracy); }
public void ScalarMultiplicationOnRight() { VectorND v1 = new VectorND(4, -8, 0); double scalar = 2; VectorND r = v1 * scalar; Assert.AreEqual(r[0], 8, required_accuracy); Assert.AreEqual(r [1], -16, required_accuracy); Assert.AreEqual(r [2], 0, required_accuracy); }
//Sets the state/initial condition. Also redefines the reset state. public void SetState(VectorND state, double t) { ivp.SetState(state, t); CurrentTime = currentTime; //Not setting a new value, but by //setting the Property we will //solve forward for some data //trailRenderer.ResetTrail(); //TODO: fix these. These were throwing a null reference exception when a new object //trailRenderer.NewTrail(); //was added, causing the AddObject function to terminate prematurely. //OR: it seems to be working fine for the moment. Should we leave this commented out? To test... }
public void SubtractDifferentDimVectors() { VectorND v1 = new VectorND(2, -4, 0); VectorND v2 = new VectorND(7, 5, -6, 3); VectorND r = v1 - v2; Assert.AreEqual(r[0], 2, required_accuracy); Assert.AreEqual(r[1], -4, required_accuracy); Assert.AreEqual(r[2], 0, required_accuracy); }
//Element-wise multiplication. Note this assumes vectors are same dimension. program carefully. public static VectorND operator*(VectorND a, VectorND b) { VectorND r = new VectorND(a.GetDim()); for (byte i = 0; i < a.GetDim(); i++) { r [i] = a [i] * b [i]; } return(r); }
//Generates a new mesh point public VectorND RungeKutta(double t, double h, VectorND v, FunctionVectorND f) { VectorND k1 = h * f.Eval(RKCallBuild(t, 0, v, 0, nullVector)); VectorND k2 = h * f.Eval(RKCallBuild(t, h / 2, v, 0.5, k1)); VectorND k3 = h * f.Eval(RKCallBuild(t, h / 2, v, 0.5, k2)); VectorND k4 = h * f.Eval(RKCallBuild(t, h, v, 1.0, k3)); return(v + (1.0 / 6.0) * (k1 + 2 * k2 + 2 * k3 + k4)); }
private void ZeroComponents(VectorND components, ref VectorND input) { for (int i = 0; i < 4; i++) { if (components.X[i] > 0) { input.X[i] = 0; } } }
//Scalar multiplication with scalar on left. public static VectorND operator*(float k, VectorND b) { VectorND r = new VectorND(b.GetDim()); for (byte i = 0; i < b.GetDim(); i++) { r [i] = k * b [i]; } return(r); }
//Forms a VectorND into a string in CSV format private string VectorNDToCSV(VectorND v) { string r = ""; for (byte i = 0; i < v.GetDim(); i++) { r += v[i] + ","; } return(r); }
public void MultiplySameDimVectors() { VectorND v1 = new VectorND(2, -4, 3, 8); VectorND v2 = new VectorND(7, 5, -6, 0); VectorND r = v1 * v2; Assert.AreEqual(r[0], 14, required_accuracy); Assert.AreEqual(r[1], -20, required_accuracy); Assert.AreEqual(r[2], -18, required_accuracy); Assert.AreEqual(r[3], 0, required_accuracy); }
/// <summary> /// This is similar logic as standard deviation, but this returns the max distance from the average /// NOTE: Spread is probably the wrong word, since this only returns the max distance (radius instead of diameter) /// </summary> public static double GetTotalSpread(IEnumerable <VectorND> values) { VectorND mean = MathND.GetCenter(values); double distancesSquared = values. Select(o => (o - mean).LengthSquared). OrderByDescending(). First(); return(Math.Sqrt(distancesSquared)); }
private byte dim; //number of dimensions //Constructor public ODEIVPSolver(byte dim_in) { //Set dimensionality dim = dim_in; //set up the null vector nullVector = new VectorND(dim); for (byte i = 0; i < dim; i++) { nullVector[i] = 0; } }
/* * Builds an array of function inputs for Runge Kutta delegate calls * The reason we need this is Bunny83's expression parser only takes * params arrays of doubles for its delegates, so we need to * concatenate t and the state vector into an array. */ private double[] RKCallBuild(double t, double t_offset, VectorND v, double v_off_scalar, VectorND v_offset) { double[] arr = new double[dim + 1]; arr[0] = t + t_offset; //t is the first value in the array for (byte i = 0; i < dim; i++) //the remaining slots are for the state vector { arr[i + 1] = v[i] + v_off_scalar * v_offset[i]; } return(arr); }
public void AddSameDimVectors() { VectorND v1 = new VectorND(1.4, 2.7, -3.1, 0); VectorND v2 = new VectorND(-3.7, 4, -1, 57.3); VectorND r = v1 + v2; Assert.AreEqual(r[0], -2.3, required_accuracy); Assert.AreEqual(r[1], 6.7, required_accuracy); Assert.AreEqual(r[2], -4.1, required_accuracy); Assert.AreEqual(r[3], 57.3, required_accuracy); }
public void SubtractSameDimVectors() { VectorND v1 = new VectorND(27.2, 2.7, -30, 0); VectorND v2 = new VectorND(-3.7, 21, -5.99, 50); VectorND r = v1 - v2; Assert.AreEqual(r[0], 30.9, required_accuracy); Assert.AreEqual(r[1], -18.3, required_accuracy); Assert.AreEqual(r[2], -24.01, required_accuracy); Assert.AreEqual(r[3], -50, required_accuracy); }
public bool CheckCipherFormat() { try { cipher = Util.ConvertStringToLatticeND(Cipher).Vectors[0]; return(true); } catch (Exception) { return(false); } }
public static string ToStringSignificantDigits(this VectorND vector, int significantDigits) { double[] arr = vector.VectorArray; if (arr == null) { return("<null>"); } return(arr. Select(o => o.ToStringSignificantDigits(significantDigits)). ToJoin(", ")); }