Exemplo n.º 1
0
    public override void Initiate(ref SparseMatrix A, ref Vector <double> B, ref Vector <double> X, int k)
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(Pos(), ReLap);
        Particle2D   p;

        for (int i = 0, j = 1; j < colliders.Length; i++)
        {
            if (colliders[i].gameObject.name.Equals(this.gameObject.name))
            {
                continue;
            }
            p = colliders[i].GetComponent <Particle2D>();
            A.At(k, p.ID, weightKernel((p.Pos() - this.Pos()).magnitude, ReLap));
            j++;
        }
        if (PND < freeSurfaceTerm)
        {
            A.At(k, this.ID, 0);
            X.At(k, 0);
            B.At(k, ((-lambda * DENSITY) / (4d * Time.fixedDeltaTime)) * (PND - defaultPND) + PND);
        }
        else
        {
            A.At(k, this.ID, -PND);
            X.At(k, 0);
            B.At(k, ((-lambda * DENSITY) / (4d * Time.fixedDeltaTime)) * (PND - defaultPND));
        }
    }
Exemplo n.º 2
0
    public override void Initiate(ref SparseMatrix A, ref Vector <double> B, ref Vector <double> X, int k)
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(Pos(), Re);
        nearbyParticles    = new Particle2D[colliders.Length];
        WEIGHTS            = new double[colliders.Length];
        nearbyParticles[0] = this;

        PND = 0;
        for (int i = 0, j = 1; j < nearbyParticles.Length; i++)
        {
            if (colliders[i].gameObject.name.Equals(this.gameObject.name))
            {
                continue;
            }
            nearbyParticles[j] = colliders[i].GetComponent <Particle2D>();
            WEIGHTS[j]         = weightKernel((nearbyParticles[j].Pos() - this.Pos()).magnitude);
            PND += WEIGHTS[j];

            A.At(k, nearbyParticles[j].ID, WEIGHTS[j]);
            j++;
        }
        if (PND < freeSurfaceTerm)
        {
            A.At(k, this.ID, -PND);
            X.At(k, 1);
            B.At(k, 0);//B.At(k,( (-lambda * DENSITY) / (4d *  Time.fixedDeltaTime) ) * (PND - defaultPND) + PND);
        }
        else
        {
            A.At(k, this.ID, -PND);
            X.At(k, 0);
            B.At(k, ((-lambda * DENSITY) / (4d * Time.fixedDeltaTime)) * (PND - defaultPND));
        }
    }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a new  <see cref="SparseMatrix"/> and inserts the given row at the given index.
        /// </summary>
        /// <param name="rowIndex">The index of where to insert the row.</param>
        /// <param name="row">The row to insert.</param>
        /// <returns>A new  <see cref="SparseMatrix"/> with the inserted column.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="row"/> is <see langword="null" />. </exception>
        /// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> is &lt; zero or &gt; the number of rows.</exception>
        /// <exception cref="ArgumentException">If the size of <paramref name="row"/> != the number of columns.</exception>
        public override Matrix <Complex> InsertRow(int rowIndex, Vector <Complex> row)
        {
            if (row == null)
            {
                throw new ArgumentNullException("row");
            }

            if (rowIndex < 0 || rowIndex > RowCount)
            {
                throw new ArgumentOutOfRangeException("rowIndex");
            }

            if (row.Count != ColumnCount)
            {
                throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row");
            }

            var result = new SparseMatrix(RowCount + 1, ColumnCount);

            for (var i = 0; i < rowIndex; i++)
            {
                result.At(i, i, At(i, i));
            }

            result.SetRow(rowIndex, row);

            for (var i = rowIndex + 1; i < result.RowCount; i++)
            {
                result.At(i, i - 1, At(i - 1, i - 1));
            }

            return(result);
        }
Exemplo n.º 4
0
    public void EmptyMatrixCreationAndCheck()
    {
        // CHeck Matrix Factory
        SparseMatrix mat = SparseMatrixFactory.CreateMatrix(10, 100);

        Assert.Equal(mat.RowCount(), 10);
        Assert.Equal(mat.ColCount(), 100);
        Assert.Equal(mat.At(0, 0), 0.0D);
        Assert.Throws <ArgumentOutOfRangeException>(() => mat.At(11, 11));
        Assert.Throws <ArgumentOutOfRangeException>(() => mat.At(-1, 11));
        Assert.Throws <ArgumentOutOfRangeException>(() => mat.At(0, 101));
        Assert.Throws <ArgumentOutOfRangeException>(() => mat.At(11, -1));
    }
Exemplo n.º 5
0
        public void TestMathNetSparseMatrix_Vecsum()
        {
            Matrix <double> matrix1 = new SparseMatrix(1024, 1024);

            // fill half the matrix
            for (int i = 0; i < 1024; i++)
            {
                for (int j = 0; j < 512; j += 2)
                {
                    matrix1.At(i, j, 1);
                }
            }

            SparseVector    inputVec = new SparseVector(1024); // all zero
            Vector <double> results  = new SparseVector(1024);

            matrix1.Multiply(inputVec, results);

            inputVec = new SparseVector(1024); // some zero
            for (int j = 0; j < 512; j += 2)
            {
                inputVec[j] = 1;
            }
            results = new SparseVector(1024);
            matrix1.Multiply(inputVec, results);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Outer product of two vectors
        /// </summary>
        /// <param name="u">First vector</param>
        /// <param name="v">Second vector</param>
        /// <returns>Matrix M[i,j] = u[i]*v[j] </returns>
        /// <exception cref="ArgumentNullException">If the u vector is <see langword="null" />.</exception>
        /// <exception cref="ArgumentNullException">If the v vector is <see langword="null" />.</exception>
        public static Matrix <Complex32> /*SparseMatrix*/ OuterProduct(SparseVector u, SparseVector v)
        {
            if (u == null)
            {
                throw new ArgumentNullException("u");
            }

            if (v == null)
            {
                throw new ArgumentNullException("v");
            }

            var matrix = new SparseMatrix(u.Count, v.Count);

            for (var i = 0; i < u.NonZerosCount; i++)
            {
                for (var j = 0; j < v.NonZerosCount; j++)
                {
                    if (u._nonZeroIndices[i] == v._nonZeroIndices[j])
                    {
                        matrix.At(i, j, u._nonZeroValues[i] * v._nonZeroValues[j]);
                    }
                }
            }

            return(matrix);
        }
Exemplo n.º 7
0
    void outPutMatrices()
    {
        using (
            var writer = new StreamWriter(File.Open(Path.Combine(Application.persistentDataPath, "Matrix data.txt"), FileMode.Create))
            )
        {
            int e = 0;
            writer.Write("Matrix A:" + Environment.NewLine);
            for (int o = 0; o < NumberOfParticles; o++)
            {
                for (int w = 0; w < NumberOfParticles; w++)
                {
                    writer.Write(A.At(o, w).ToString("#.###0") + " ");
                }
                writer.Write(Environment.NewLine);
            }

            writer.Write("Vector B is:" + Environment.NewLine);
            for (int o = 0; o < NumberOfParticles; o++)
            {
                writer.Write(B.At(o).ToString("#.###0") + " ");
            }
            writer.Write(Environment.NewLine + "Vector X is:" + Environment.NewLine);
            for (int o = 0; o < NumberOfParticles; o++)
            {
                writer.Write(X.At(o).ToString("#.##0") + " ");
            }
        }
    }
Exemplo n.º 8
0
        /// <summary>
        /// Outer product of two vectors
        /// </summary>
        /// <param name="u">First vector</param>
        /// <param name="v">Second vector</param>
        /// <returns>Matrix M[i,j] = u[i]*v[j] </returns>
        /// <exception cref="ArgumentNullException">If the u vector is <see langword="null" />.</exception>
        /// <exception cref="ArgumentNullException">If the v vector is <see langword="null" />.</exception>
        public static Matrix <float> OuterProduct(SparseVector u, SparseVector v)
        {
            if (u == null)
            {
                throw new ArgumentNullException("u");
            }

            if (v == null)
            {
                throw new ArgumentNullException("v");
            }

            var matrix = new SparseMatrix(u.Count, v.Count);

            for (var i = 0; i < u._storage.ValueCount; i++)
            {
                for (var j = 0; j < v._storage.ValueCount; j++)
                {
                    if (u._storage.Indices[i] == v._storage.Indices[j])
                    {
                        matrix.At(i, j, u._storage.Values[i] * v._storage.Values[j]);
                    }
                }
            }

            return(matrix);
        }
        public Tuple <double, double> GetValue(int i, int j)
        {
            if (i >= 0 && j >= 0 && i < storage.RowCount && j < storage.ColumnCount)
            {
                return(new Tuple <double, double>(matrix.At(i, j), 0.0));
            }

            return(new Tuple <double, double>(0.0, 0.0));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Get the 1D Laplacian matrix (with Dirichlet boundary conditions).
        /// </summary>
        /// <param name="nx">Grid size.</param>
        /// <param name="eigenvalues">Vector to store eigenvalues (optional).</param>
        /// <returns>Laplacian sparse matrix.</returns>
        public static SparseMatrix Laplacian(int nx, DenseVector eigenvalues = null)
        {
            if (nx == 1)
            {
                // Handle special case n = 1.
                var A = new SparseMatrix(nx, nx);

                A.At(0, 0, 2.0);

                return(A);
            }

            var C = new CoordinateStorage <Complex>(nx, nx);

            for (int i = 0; i < nx; i++)
            {
                C.At(i, i, 2.0);

                if (i == 0)
                {
                    C.At(i, i + 1, -1.0);
                }
                else if (i == (nx - 1))
                {
                    C.At(i, i - 1, -1.0);
                }
                else
                {
                    C.At(i, i - 1, -1.0);
                    C.At(i, i + 1, -1.0);
                }
            }

            if (eigenvalues != null)
            {
                // Compute eigenvalues.
                int count = Math.Min(nx, eigenvalues.Count);

                var eigs = new double[nx];

                for (int i = 0; i < count; i++)
                {
                    eigs[i] = 4 * Math.Pow(Math.Sin((i + 1) * Math.PI / (2 * (nx + 1))), 2);
                }

                Array.Sort(eigs);

                for (int i = 0; i < count; ++i)
                {
                    eigenvalues.At(i, eigs[i]);
                }
            }

            return((SparseMatrix)C.ToSparseMatrix());
        }
Exemplo n.º 11
0
        /// <summary>
        /// Create a matrix based on this vector in row form (one single row).
        /// </summary>
        /// <returns>This vector as a row matrix.</returns>
        public override Matrix <Complex32> ToRowMatrix()
        {
            var matrix = new SparseMatrix(1, Count);

            for (var i = 0; i < NonZerosCount; i++)
            {
                matrix.At(0, _nonZeroIndices[i], _nonZeroValues[i]);
            }

            return(matrix);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Create a matrix based on this vector in column form (one single column).
        /// </summary>
        /// <returns>This vector as a column matrix.</returns>
        public override Matrix <Complex32> ToColumnMatrix()
        {
            var matrix = new SparseMatrix(Count, 1);

            for (var i = 0; i < NonZerosCount; i++)
            {
                matrix.At(_nonZeroIndices[i], 0, _nonZeroValues[i]);
            }

            return(matrix);
        }
Exemplo n.º 13
0
        public Tuple <double, double> GetValue(int i, int j)
        {
            if (i >= 0 && j >= 0 && i < storage.RowCount && j < storage.ColumnCount)
            {
                var value = matrix.At(i, j);

                return(new Tuple <double, double>(value.Real, value.Imaginary));
            }

            return(new Tuple <double, double>(0.0, 0.0));
        }
Exemplo n.º 14
0
    public void Initate(ref SparseMatrix A, ref Vector <double> B, ref Vector <double> X, int k)
    {
        Collider[] colliders = Physics.OverlapSphere(Pos(), InteractionRadius);
        nearbyParticles    = new Particle[colliders.Length];
        weights            = new double[nearbyParticles.Length];
        nearbyParticles[0] = this;

        particleDensity = 0;

        for (int i = 0, j = 1; j < nearbyParticles.Length; i++)
        {
            if (colliders[i].gameObject.name.Equals(this.gameObject.name))
            {
                continue;
            }
            nearbyParticles[j] = colliders[i].GetComponent <Particle>();
            weights[j]         = weightKernel(nearbyParticles[j].Pos());
            particleDensity   += weights[j];

            A.At(k, nearbyParticles[j].ID, weights[j]);
            j++;
        }
        if (particleDensity < freeSurfaceTerm)
        {
            A.At(k, this.ID, 0);
            X.At(k, 1d);
            //B.At(k, term2 * ((particleDensity - defaultParticleDensity) / defaultParticleDensity) +
            //    ((6d*particleDensity) / (lambda*defaultParticleDensity)));
            B.At(k, (-lambda * DENSITY * (particleDensity - defaultParticleDensity)) / (6d * Time.fixedDeltaTime * Time.fixedDeltaTime) + particleDensity);
            //B.At(k, 0);
        }
        else
        {
            A.At(k, this.ID, -particleDensity);
            X.At(k, 0);
            //B.At(k, term2 * ((particleDensity - defaultParticleDensity) / defaultParticleDensity));

            B.At(k, (-lambda * DENSITY * (particleDensity - defaultParticleDensity)) / (6d * Time.fixedDeltaTime * Time.fixedDeltaTime));
        }
    }
Exemplo n.º 15
0
 public double At(int i, int j)
 {
     return(readOnlyMatrix.At(rowMappings[i], j));
 }
Exemplo n.º 16
0
    public void MatrixCheck(SparseMatrix sm)
    {
        Assert.Equal(sm.RowCount(), 10);
        Assert.Equal(sm.ColCount(), 100);
        Assert.Equal(sm.At(0, 0), 0.0D);

        Assert.Throws <ArgumentOutOfRangeException>(() => sm.At(11, 11));
        Assert.Throws <ArgumentOutOfRangeException>(() => sm.At(-1, 11));
        Assert.Throws <ArgumentOutOfRangeException>(() => sm.At(0, 101));
        Assert.Throws <ArgumentOutOfRangeException>(() => sm.At(11, -1));


        // Set Values and Retrieve
        sm.At(9, 9, 10.0);
        Assert.Equal(10.0D, sm.At(9, 9));
        Assert.Equal(0.0D, sm.At(9, 10));

        // Transpose Checks
        sm.At(2, 11, 11.0);
        SparseMatrix t = sm.Transpose();

        Assert.Equal(10.0D, t.At(9, 9));
        Assert.Equal(0.0D, t.At(9, 10));
        Assert.Equal(11.0D, t.At(11, 2));
        Assert.Equal(10, t.ColCount());
        Assert.Equal(100, t.RowCount());

        // Calculations Check: dot, axpy
        double[] w = new double[100];
        for (int i = 0; i < 10; i++)
        {
            sm.At(1, i, (double)i + 1);
            w[i] = (double)10 - i;
        }

        w[10] = 40.0;

        // nrm2_sq
        Assert.Equal(385, sm.nrm2_sq(1));

        // dot
        Assert.Equal(220, sm.dot(1, w));
        sm.At(1, 10, 1);
        Assert.Equal(260, sm.dot(1, w));

        // axpy(double a, int row, double[] y)
        for (int i = 0; i < 100; i++)
        {
            w[i] = 0;
        }

        sm.axpy(10, 1, w);
        Assert.Equal(30, w[2]);
        Assert.Equal(10, w[10]);
        Assert.Equal(0, w[11]);


        // IEnumerable<SparseRowValue> getRow(int row);
        IEnumerable <SparseRowValue> vr = sm.getRow(1);
        int k = 0;

        foreach (SparseRowValue sr in vr)
        {
            k++;
        }
        Assert.Equal(11, k);

        vr = sm.getRow(7);
        k  = 0;
        foreach (SparseRowValue sr in vr)
        {
            k++;
        }
        Assert.Equal(0, k);

        // public RowArrayPtr<SparseRowValue> getRowPtr(int row)
        RowArrayPtr <SparseRowValue> rowPtr = sm.getRowPtr(1);

        Assert.Equal(11, rowPtr.Length);
        rowPtr = sm.getRowPtr(7);
        Assert.Equal(0, rowPtr.Length);

        SparseMatrix cpSm = (SparseMatrix)Activator.CreateInstance(sm.GetType(), new object[] { sm.RowCount(), sm.ColCount() });

        cpSm.CopyRow(3, sm, 1);
        Assert.Equal(cpSm.RowCount(), 10);
        Assert.Equal(cpSm.ColCount(), 100);
        Assert.Equal(cpSm.At(0, 0), 0.0D);
        rowPtr = cpSm.getRowPtr(3);
        Assert.Equal(11, rowPtr.Length);
        Assert.Equal(386, cpSm.nrm2_sq(3));

        // TODO: LoadRow.
    }
Exemplo n.º 17
0
 /// <inheritdoc />
 public double GetValue(int row, int column)
 {
     return(_sparseMatrix.At(row, column));
 }