Ejemplo n.º 1
0
        private static LibMatrix Cast(ISuperDuperMatrix other)
        {
            if (other is not LibMatrix libMatrix)
            {
                throw new NotSupportedException("Other should be The Lib Matrix too");
            }

            return(libMatrix);
        }
        private static Vector Cast(ISuperDuperMatrix other)
        {
            if (other is not Vector vector)
            {
                throw new NotSupportedException("Other should be the Vector too");
            }

            return(vector);
        }
        private static DiagonalMatrix Cast(ISuperDuperMatrix other)
        {
            if (other is not DiagonalMatrix diagonalMatrix)
            {
                throw new NotSupportedException("Other should be Diagonal too");
            }

            return(diagonalMatrix);
        }
        public ISuperDuperMatrix Add(ISuperDuperMatrix other)
        {
            var added = Cast(other)._diagonal;

            for (var i = 0; i < _diagonal.Length; i++)
            {
                added[i] += _diagonal[i];
            }
            return(new DiagonalMatrix(added));
        }
        public ISuperDuperMatrix Subtract(ISuperDuperMatrix other)
        {
            var subtracted = Cast(other)._diagonal;

            for (var i = 0; i < _diagonal.Length; i++)
            {
                subtracted[i] = _diagonal[i] - subtracted[i];
            }
            return(new DiagonalMatrix(subtracted));
        }
 public Function(Type type, int n, double[] a, double[] b, double c)
 {
     // TODO check dimensions
     Type = type;
     N    = n;
     _a   = AdvancedMath.ToMatrix(type, n, a);
     A    = _a.ComputeA();
     _b   = AdvancedMath.ToVector(n, b);
     _c   = c;
 }
        public ISuperDuperMatrix Multiply(ISuperDuperMatrix other)
        {
            var isVector   = other is Vector;
            var multiplied = isVector ? ((Vector)other).ToVector() : Cast(other)._diagonal;

            for (var i = 0; i < _diagonal.Length; i++)
            {
                multiplied[i] *= _diagonal[i];
            }
            return(isVector ? new Vector(multiplied) : new DiagonalMatrix(multiplied));
        }
Ejemplo n.º 8
0
        public ISuperDuperMatrix ComputeA()
        {
            var vA = new double[N * N];

            for (var i = 0; i < N; i++)
            {
                for (var j = 0; j < N; j++)
                {
                    vA[i * N + j] = 0.5 * (Get(i, j) + Get(j, i));
                }
            }

            return(ISuperDuperMatrix.Create(ISuperDuperMatrix.LIB_MATRIX, N, N, vA));
        }
Ejemplo n.º 9
0
        public ISuperDuperMatrix Multiply(ISuperDuperMatrix other)
        {
            switch (other)
            {
            case Vector vector:
                return(new Vector(Matrix.Multiply(vector.MatrixVector).ToColumnArrays()[0]));

            case DiagonalMatrix diagonalMatrix:
            {
                var vectorT = Matrix.ToRowArrays()[0];
                for (var i = 0; i < vectorT.Length; i++)
                {
                    vectorT[i] *= diagonalMatrix.Get(i, i);
                }

                return(new LibMatrix(1, vectorT.Length, vectorT));
            }

            default:
                return(new LibMatrix(Matrix.Multiply(Cast(other).Matrix)));
            }
        }
Ejemplo n.º 10
0
 public ISuperDuperMatrix Add(ISuperDuperMatrix other)
 {
     return(new LibMatrix(Matrix.Add(Cast(other).Matrix)));
 }
Ejemplo n.º 11
0
 public ISuperDuperMatrix Subtract(ISuperDuperMatrix other)
 {
     return(new LibMatrix(Matrix.Subtract(Cast(other).Matrix)));
 }
 public ISuperDuperMatrix Gradient(ISuperDuperMatrix x)
 {
     return(A.Multiply(x).Subtract(_b));
 }
 public double Apply(ISuperDuperMatrix x)
 {
     return(0.5 * x.Transpose().Multiply(_a).Multiply(x).Get(0, 0) -
            _b.Transpose().Multiply(x).Get(0, 0) + _c);
 }
Ejemplo n.º 14
0
 public ISuperDuperMatrix Add(ISuperDuperMatrix other)
 {
     return(new Vector(MatrixVector.Add(Cast(other).MatrixVector)));
 }
Ejemplo n.º 15
0
 public ISuperDuperMatrix Subtract(ISuperDuperMatrix other)
 {
     return(new Vector(MatrixVector.Subtract(Cast(other).MatrixVector)));
 }
Ejemplo n.º 16
0
 public ISuperDuperMatrix Multiply(ISuperDuperMatrix other)
 {
     throw new NotSupportedException("Cannot multiply");
 }