public void Test_Merge() { var input = new Array3D(new double[, , ] { { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }, { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }, { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } } }); var filter = new Array3D(new double[, , ] { { { 1.1, 1.2, 1.3 }, { 1.4, 1.5, 1.6 } }, { { 1.1, 1.2, 1.3 }, { 1.4, 1.5, 1.6 } }, }); var result = Array3D.Merge(input, filter, 0, 0); var result2 = Array3D.Merge(input, filter, 1, 1); }
public override Array3D Train(Array3D input, Array3D error, Array3D output) { var delta = new Array3D(error.Height, error.Width, error.Depth); for (var i = 0; i < delta.Height; i++) { for (var j = 0; j < delta.Width; j++) { for (var k = 0; k < delta.Depth; k++) { delta[i, j, k] = error[i, j, k] * ActivationFunction.Derivative(output[i, j, k]); } } } for (var i = 0; i < output.Count; i++) { WeightAccumulator[i] += input * (delta[i] * LearningRate); BiasAccumulator[i] += delta[i] * LearningRate; } var newError = CalculateError ? new Array3D(input.Height, input.Width, input.Depth) : null; if (CalculateError) { for (var i = 0; i < output.Count; i++) { newError += Neurons[i].Weights * delta[i]; } } return(newError); }
private void initDefaultWorld() { chunks = new Array3D <Chunk>(NumChunks); var totalTime = new TimeSpan(0); var i = 0; chunks.ForEach((c, p) => { i++; totalTime.Add(PerformanceHelper.Measure(() => { var grid = HermiteDataGrid.CopyGrid(new DensityFunctionHermiteGrid(v => { v += (Vector3)p.ToVector3() * chunkSize; return(20 - v.Y); }, new Point3(chunkSize + 1, chunkSize + 1, chunkSize + 1))); var chunk = new Chunk(p); chunk.SetGrid(grid); chunk.UpdateSurface(surfaceRenderer); chunks[p] = chunk; })); }); totalTime.Multiply(1f / i); }
/// <summary> /// Computes part of <see cref="Volume(ref Array3D{long})"/> that doesn't depend on <see cref="RMax"/>, <see cref="GMax"/> /// or <see cref="BMax"/>, depending on <paramref name="dir"/>. /// </summary> internal long Bottom(Direction dir, ref Array3D <long> mmt) { switch (dir) { case Direction.Red: return(-mmt[RMin, GMax, BMax] + mmt[RMin, GMax, BMin] + mmt[RMin, GMin, BMax] - mmt[RMin, GMin, BMin]); case Direction.Green: return(-mmt[RMax, GMin, BMax] + mmt[RMax, GMin, BMin] + mmt[RMin, GMin, BMax] - mmt[RMin, GMin, BMin]); case Direction.Blue: return(-mmt[RMax, GMax, BMin] + mmt[RMax, GMin, BMin] + mmt[RMin, GMax, BMin] - mmt[RMin, GMin, BMin]); default: // Just to satisfy the compiler. No resource is needed, cannot occur. throw new ArgumentOutOfRangeException(nameof(dir)); } }
private void Initialize() { var image = Image.FromFile(TrainPath); Bitmap bitmap; if (ImageSize.Height == 0 || ImageSize.Width == 0) { bitmap = new Bitmap(image, image.Width, image.Height); } else { bitmap = new Bitmap(image, ImageSize.Width, ImageSize.Height); } var array = new Array3D(bitmap.Height, bitmap.Width, 3); for (var i = 0; i < bitmap.Height; i++) { for (var j = 0; j < bitmap.Width; j++) { var pixel = bitmap.GetPixel(j, i); array[i, j, 0] = pixel.R / 255.0; array[i, j, 1] = pixel.G / 255.0; array[i, j, 2] = pixel.B / 255.0; } } _pair = new TrainingData <Array3D, Array3D> { Expected = _expected, Input = array }; }
public Array3D <byte> Generate() { var result = new Array3D <byte>(width, depth, Math.Max(height, 2)); var random = new Random(); for (var i = 0; i < iteratirons; i++) { if (i == 0) { for (var c1 = 0; c1 < width; c1++) { for (var c2 = 0; c2 < depth; c2++) { result[c1, c2, 0] = (byte)BlockType.Dirt; } } } else { var hillParams = new IntPoint3D(random.Next(width), random.Next(depth), random.Next(height)); CreateHill(result, hillParams); } } return(result); }
public void Load(FileInfo fi) { using (var fs = new BinaryReader(fi.OpenRead())) { var format = fs.ReadString(); var size = new Point3(fs.ReadInt32(), fs.ReadInt32(), fs.ReadInt32()); cells = new Array3D <Vertex>(size); for (int x = 0; x < cells.Size.X; x++) { for (int y = 0; y < cells.Size.Y; y++) { for (int z = 0; z < cells.Size.Z; z++) { var val = new Vertex(); val.Sign = fs.ReadBoolean(); val.EdgeData = new Vector4[fs.ReadInt32()]; for (int i = 0; i < val.EdgeData.Length; i++) { val.EdgeData[i] = new Vector4(fs.ReadSingle(), fs.ReadSingle(), fs.ReadSingle(), fs.ReadSingle()); } cells[new Point3(x, y, z)] = val; } } } } }
private void CreateHill(Array3D <byte> data, IntPoint3D hillParams) { var c1Min = hillParams.X - hillParams.Z < 0 ? 0 : hillParams.X - hillParams.Z; var c1Max = Math.Min(hillParams.X + hillParams.Z, width - 1); var c2Min = hillParams.Y - hillParams.Z < 0 ? 0 : hillParams.Y - hillParams.Z; var c2Max = Math.Min(hillParams.Y + hillParams.Y, depth - 1); for (var c1 = c1Min; c1 <= c1Max; c1++) { for (var c2 = c2Min; c2 <= c2Max; c2++) { var c3Max = (int)Math.Sqrt(hillParams.Z * hillParams.Z - (c1 - hillParams.X) * (c1 - hillParams.X) - (c2 - hillParams.Y) * (c2 - hillParams.Y)); if (c3Max <= 0) { continue; } for (var i = 0; i < c3Max; i++) { data[c1, c2, i] = (byte)BlockType.Dirt; } } } }
public override Array3D Train(Array3D input, Array3D error, Array3D output) { var delta = new Array3D(error.Count); for (var i = 0; i < delta.Count; i++) { delta[i] = error[i] * ActivationFunction.Derivative(output[i]); } for (var i = 0; i < output.Count; i++) { PrevChange[i] = input * delta[i] * LearningRate + PrevChange[i] * Momentum; Neurons[i].Weights -= PrevChange[i]; Neurons[i].Bias -= delta[i] * LearningRate; } var newError = new Array3D(input.Count); for (var i = 0; i < output.Count; i++) { newError += Neurons[i].Weights * delta[i]; } return(newError); }
public float sampleTrilinear(Array3D <float> noise, Vector3 pos) { var min = pos.ToFloored(); var f = pos - min; /*var q000 = noise.GetTiled(new Point3(0, 0, 0) + min); * var q100 = noise.GetTiled(new Point3(1, 0, 0) + min); * var q010 = noise.GetTiled(new Point3(0, 1, 0) + min); * var q001 = noise.GetTiled(new Point3(0, 0, 1) + min); * var q110 = noise.GetTiled(new Point3(1, 1, 0) + min); * var q011 = noise.GetTiled(new Point3(0, 1, 1) + min); * var q101 = noise.GetTiled(new Point3(1, 0, 1) + min); * var q111 = noise.GetTiled(new Point3(1, 1, 1) + min);*/ var x0 = TWMath.nfmod(min.X + 0, noise.Size.X);; var y0 = TWMath.nfmod(min.Y + 0, noise.Size.Y); var z0 = TWMath.nfmod(min.Z + 0, noise.Size.Z); var x1 = TWMath.nfmod(x0 + 1, noise.Size.X); var y1 = TWMath.nfmod(y0 + 1, noise.Size.Y); var z1 = TWMath.nfmod(z0 + 1, noise.Size.Z); var q000 = noise.GetFast(x0, y0, z0); var q100 = noise.GetFast(x1, y0, z0); var q010 = noise.GetFast(x0, y1, z0); var q001 = noise.GetFast(x0, y0, z1); var q110 = noise.GetFast(x1, y1, z0); var q011 = noise.GetFast(x0, y1, z1); var q101 = noise.GetFast(x1, y0, z1); var q111 = noise.GetFast(x1, y1, z1); var ret = TWMath.triLerp(f.dx(), q000, q100, q001, q101, q010, q110, q011, q111); return(ret); }
private bool[,,] SearchCubeThatMustBeRemoved() { bool[,,] mustBeRemovedCubes = new bool[this.GetSizeX(), this.GetSizeY(), this.GetSizeZ()]; Array3D <GameObject> pool = new Array3D <GameObject>(blockPool); Array3D <bool> checkedPool = new Array3D <bool>(mustBeRemovedCubes); for (int z = 0; z < this.GetSizeZ(); z++) { for (int y = 0; y < this.GetSizeY(); y++) { if (pool.TakeXRow(y, z).All(e => e != null)) { checkedPool.SetXRow(y, z, true); } } } for (int x = 0; x < this.GetSizeX(); x++) { for (int y = 0; y < this.GetSizeY(); y++) { if (pool.TakeZRow(x, y).All(e => e != null)) { checkedPool.SetZRow(x, y, true); } } } return(checkedPool.GetArray3D()); }
static void Main(string[] args) { int N = 100; Array3D <double> array3D = new Array3D <double>(N, N, N); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) { array3D[i, j, k] = 666; } } } string str = "Hello World"; InputForTemp3D input = new InputForTemp3D(); input.InputMessage = str; input.C = 3; input.Tau = 0.001; input.H = 0.1; input.TimeSteps = 100; input.U = array3D; ServiceClient client = new ServiceClient(); OutputForTemp3D output = client.CalculateTemp3D(input); Console.WriteLine(output.OutputMessage); Console.ReadKey(); }
private int CountCompletedRow(bool[,,] checkedPool) { int completedRowNum = 0; Array3D <bool> pool = new Array3D <bool>(checkedPool); for (int z = 0; z < this.GetSizeZ(); z++) { for (int y = 0; y < this.GetSizeY(); y++) { if (pool.TakeXRow(y, z).All(e => e == true)) { completedRowNum++; } } } for (int x = 0; x < this.GetSizeX(); x++) { for (int y = 0; y < this.GetSizeY(); y++) { if (pool.TakeZRow(x, y).All(e => e == true)) { completedRowNum++; } } } return(completedRowNum); }
public override TrainingData <Array3D, Array3D> GetNext(bool isTraining) { var imageFullPath = isTraining ? TrainPath : TestPath; var bitmap = new Bitmap(Image.FromFile(imageFullPath)); var column = rand.Next(_imageCount.Width); var row = rand.Next(_imageCount.Height); var origin = new Size { Width = _margin.Width + column * (_padding.Width + ImageSize.Width) + _padding.Width, Height = _margin.Height + row * (_padding.Height + ImageSize.Height) + _padding.Height }; var array = new Array3D(bitmap.Height, bitmap.Width, 3); for (var i = 0; i < bitmap.Height; i++) { for (var j = 0; j < bitmap.Width; j++) { var pixel = bitmap.GetPixel(j, i); array[origin.Height + i, origin.Width + j, 0] = pixel.R; array[origin.Height + i, origin.Width + j, 1] = pixel.G; array[origin.Height + i, origin.Width + j, 2] = pixel.B; } } //var pair = new TrainingData<Array3D, Array> { Expected = Classes[row], Input = array }; var pair = new TrainingData <Array3D, Array3D> { Expected = new Array3D(), Input = array }; return(pair); }
private double GetAverage(Array3D <double> currentArray, int i, int j, int k, int perNodeX, int perNodeY, int perNodeZ) { double perX = 0; double perY = 0; double perZ = 0; for (int l = 0; l < perNodeX; l++) { perX += currentArray[i + l, j, k]; } perX = perX / perNodeX; for (int l = 0; l < perNodeY; l++) { perY += currentArray[i, j + l, k]; } perY = perY / perNodeY; for (int l = 0; l < perNodeZ; l++) { perZ += currentArray[i, j, k + l]; } perZ = perZ / perNodeZ; return((perX + perY + perZ) / 3.0); }
public static Func <Vector3, float> createDensityFunction5Perlin(int seed, int height) { Array3D <float> noise = generateNoise(seed); /*noise = new Array3D<float>(new Point3(2, 2, 2)); * noise[new Point3(1, 1, 1)] = 3; * noise[new Point3(0, 0, 0)] = -3;*/ var seeder = new Seeder(0); var sampler = new Array3DSampler <float>(); Func <Vector3, float> densityFunction = v => { var density = (float)height - v.Y; v *= 1 / 8f; //v *= (1/8f); density += sampler.sampleTrilinear(noise, v * 4.03f) * 0.25f; density += sampler.sampleTrilinear(noise, v * 1.96f) * 0.5f; density += sampler.sampleTrilinear(noise, v * 1.01f) * 1; density += sampler.sampleTrilinear(noise, v * 0.55f) * 10; density += sampler.sampleTrilinear(noise, v * 0.21f) * 30; //density += noise.GetTiled(v.ToFloored()); return(density); }; return(densityFunction); }
public void TestDimentions() { Array3D empty = new Array3D(); Array3D array = new Array3D(20, 12, 3); Assert.AreEqual(array.Height, 20); Assert.AreEqual(array.Width, 12); Assert.AreEqual(array.Depth, 3); }
public void ShouldThrowArgumentExceptionIfGetSubArrayDimension1IndexLtZero() { var subject = new Array3D(_working3DArray); Assert.Throws( typeof(ArgumentException), () => subject.GetSubArray(-1) ); }
public void ZeroDimensionTest() { var array3D = new Array3D <int>(0, 2, 1); Assert.IsTrue(array3D.IsNullOrEmpty()); Assert.AreEqual(Reflector.EmptyArray <int>(), array3D.Buffer.ToArray()); int _; Assert.Throws <IndexOutOfRangeException>(() => _ = array3D.Buffer[0]); }
/// <summary> /// Constructor building the propagator and initializing compatible. /// </summary> /// <param name="waveHeight">The wave's height.</param> /// <param name="waveWidth">The wave's width.</param> /// <param name="periodicOutput"><c>true</c> if the wave and the output is toric.</param> /// <param name="propagatorState">The propagator state.</param> public Propagator(uint waveHeight, uint waveWidth, bool periodicOutput, PropagatorState <uint> propagatorState) { _PatternsSize = propagatorState.PatternCount; _WaveHeight = waveHeight; _WaveWidth = waveWidth; _PeriodicOutput = periodicOutput; _PropagatorState = propagatorState; _Propagating = new Stack <PropagationEntry>(); _Compatible = new Array3D <int[]>(waveHeight, waveWidth, _PatternsSize); InitCompatible(); }
public void Test_SoftMax_Function() { var input = new Array3D(new List <double> { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); var softMax = new SoftMaxFunction(); var result = softMax.Activate(input); Assert.AreEqual(result.WeightSum(), 1, 0.00001); }
/// <summary> /// Computes the sum over a box of any given statistic. /// </summary> internal long Volume(ref Array3D <long> mmt) { return(mmt[RMax, GMax, BMax] - mmt[RMax, GMax, BMin] - mmt[RMax, GMin, BMax] + mmt[RMax, GMin, BMin] - mmt[RMin, GMax, BMax] + mmt[RMin, GMax, BMin] + mmt[RMin, GMin, BMax] - mmt[RMin, GMin, BMin]); }
/// <summary> /// Computes the sum over a box of any given statistic (floating point version). /// </summary> internal float Volume(ref Array3D <float> mmt) { return(mmt[RMax, GMax, BMax] - mmt[RMax, GMax, BMin] - mmt[RMax, GMin, BMax] + mmt[RMax, GMin, BMin] - mmt[RMin, GMax, BMax] + mmt[RMin, GMax, BMin] + mmt[RMin, GMin, BMax] - mmt[RMin, GMin, BMin]); }
public void TestDensityCalcPerformance() { var dens = VoxelTerrainGenerationTest.createDensityFunction5Perlin(11, 10); var times = 10000; var s = new Stopwatch(); s.Start(); for (int i = 0; i < times; i++) { float a = dens(new Vector3(1)); } s.Stop(); var densityEval = s.Elapsed.TotalSeconds / times; Console.WriteLine("Density evaluation: 100% - {0} ms", +densityEval * 1000); s.Reset(); s.Start(); for (int i = 0; i < times * 5; i++)// 5 trilerps per density lookup { float a = TWMath.triLerp(new Vector3(0.5f), 4, 7, 8, 6, 4, 8, 9, 7); } s.Stop(); var trilerp = s.Elapsed.TotalSeconds / times; Console.WriteLine("Trilerp : {0:###}% - {1}ms", trilerp / densityEval * 100, trilerp * 1000); var ar = new Array3D <float>(new Point3(16, 16, 16)); s.Reset(); s.Start(); for (int i = 0; i < times * 5 * 8; i++)// 8 getTiled per trilerp and 5 trilerps per density lookup { /*var pos = new Point3(5, 5, 5); * pos.X = TWMath.nfmod(pos.X, ar.Size.X); * pos.Y = TWMath.nfmod(pos.Y, ar.Size.Y); * pos.Z = TWMath.nfmod(pos.Z, ar.Size.Z); * float a = ar.arr[pos.X, pos.Y, pos.Z];*/ float a = ar.GetTiled(new Point3(5, 5, 5)); } s.Stop(); var getTiled = s.Elapsed.TotalSeconds / times; Console.WriteLine("GetTiled : {0:###}% - {1}ms", getTiled / densityEval * 100, getTiled * 1000); }
private void Draw() { if (MaxU == -10000000 || MinU == -10000000) { DefineMaxMin(); } double l = 5; double interval = (MaxU - MinU) / l; ModelArea.Children.Clear(); Array3D <double> arrayToDraw = U; for (int i = 0; i < U.XLength; i++) { for (int j = 0; j < U.XLength; j++) { for (int k = 0; k < U.XLength; k++) { GeometryModel3D geom; var builder = new MeshBuilder(true, true); var position = new Point3D(i, j, k); builder.AddSphere(position, 0.25, 15, 15); var visual = new ModelVisual3D(); if (U[i, j, k] < MinU + interval) { geom = new GeometryModel3D(builder.ToMesh(), Materials.Blue); } else if (U[i, j, k] >= MinU + interval && U[i, j, k] < MinU + 2 * interval) { geom = new GeometryModel3D(builder.ToMesh(), Materials.Yellow); } else if (U[i, j, k] >= MinU + 2 * interval && U[i, j, k] < MinU + 3 * interval) { geom = new GeometryModel3D(builder.ToMesh(), Materials.Gold); } else if (U[i, j, k] >= MinU + 3 * interval && U[i, j, k] < MinU + 4 * interval) { geom = new GeometryModel3D(builder.ToMesh(), Materials.Orange); } else { geom = new GeometryModel3D(builder.ToMesh(), Materials.Red); } visual.Content = geom; ModelArea.Children.Add(visual); } } } }
public void ShouldPullOutProperSubArrayStatic() { var subArray = Array3D.GetSubArray(_working3DArray, 1); var expectedResults = new Array2D(new byte[3, 4] { { 13, 14, 15, 16 }, { 17, 18, 19, 20 }, { 21, 22, 23, 24 } }); Assert.AreEqual(expectedResults.Array, subArray); }
public static Array3D <float> generateNoise(int seed) { var ret = new Array3D <float>(new Point3(16, 16, 16)); var r = new Seeder(seed); ret.ForEach((val, p) => { ret[p] = r.NextFloat(-1, 1); }); return(ret); }
static public Array3D <double> CalcNewTN3D(Array3D <double> U, double a, double h, double tau, int steps) { double R = a * a * tau / h / h; /// int k = 0; do { U = ProgonkaPPM3D(R, U); k++; } while (k < steps); return(U); }
public void Test_Return_Correct_Output_If_Input_Not_Resized() { var inputArray = new double[, , ] { { { 3, 10, 2, 4 }, { 5, 11, 8, 7 }, { 3, 10, 5, 3 }, { 5, 1, 2, 3 } }, { { 3, 10, 2, 4 }, { 5, 11, 8, 7 }, { 3, 10, 7, 3 }, { 5, 1, 3, 3 } }, { { 3, 10, 2, 4 }, { 5, 11, 8, 7 }, { 3, 4, 5, 3 }, { 5, 1, 2, 3 } }, { { 3, 10, 2, 4 }, { 5, 11, 8, 7 }, { 3, 5, 5, 3 }, { 5, 1, 2, 3 } } }; var expectedArray = new double[, , ] { { { 5, 11, 8, 7 }, { 5, 10, 7, 3 } }, { { 5, 11, 8, 7 }, { 5, 5, 5, 3 } } }; var input = new Array3D(inputArray); var expected = new Array3D(expectedArray); var pooling = new PoolingLayer(); var result = pooling.FormOutput(input); Assert.IsTrue(result as Array3D == expected); }
public void TestFormOutput() { var rand = new Random(); var perceptron = new Perceptron(5); var array = new Array3D(5.0); for (var i = 0; i < 5; i++) { array[i] = rand.NextDouble(); } var result = perceptron.FormOutput(array); Assert.IsTrue(result < 1); }
private bool[,,] SearchCubeThatMustBeRemoved() { bool[,,] mustBeRemovedCubes = new bool[this.GetSizeX(), this.GetSizeY(), this.GetSizeZ()]; Array3D<GameObject> pool = new Array3D<GameObject>(blockPool); Array3D<bool> checkedPool = new Array3D<bool>(mustBeRemovedCubes); for (int z = 0; z < this.GetSizeZ(); z++) for (int y = 0; y < this.GetSizeY(); y++) if (pool.TakeXRow(y, z).All(e => e != null)) checkedPool.SetXRow(y, z, true); for (int x = 0; x < this.GetSizeX(); x++) for (int y = 0; y < this.GetSizeY(); y++) if (pool.TakeZRow(x, y).All(e => e != null)) checkedPool.SetZRow(x, y, true); return checkedPool.GetArray3D(); }
private int CountCompletedRow(bool[,,] checkedPool) { int completedRowNum = 0; Array3D<bool> pool = new Array3D<bool>(checkedPool); for (int z = 0; z < this.GetSizeZ(); z++) for (int y = 0; y < this.GetSizeY(); y++) if (pool.TakeXRow(y, z).All(e => e == true)) completedRowNum++; for (int x = 0; x < this.GetSizeX(); x++) for (int y = 0; y < this.GetSizeY(); y++) if (pool.TakeZRow(x, y).All(e => e == true)) completedRowNum++; return completedRowNum; }
private void DoScan(int sleep) { this.cancel = false; this.processing = true; this.objects = new Array3D<GameObject>(this.map.Columns, this.map.Rows, Math.Max(this.map.Layers.Length, 1)); var transform = this.map.transform; for (var i = 0; i < transform.childCount; i++) { // process child var child = transform.GetChild(i); string prefabName; int layer; int column; int row; if (child.name.TryParsePrefabName(out prefabName, out layer, out column, out row)) { this.Set(column, row, layer, child.gameObject); } // check if canceled if (this.cancel) { break; } // update progress this.progress = (float)i / transform.childCount; // check if threat should sleep between each entry if (sleep > 0) { System.Threading.Thread.Sleep(sleep); } } this.cancel = false; this.processing = false; }