Exemple #1
0
Fichier : vml.cs Projet : 0xCM/z0
 public static ref Matrix256 <M, N, float> trunc <M, N>(ref Matrix256 <M, N, float> A)
     where M : unmanaged, ITypeNat
     where N : unmanaged, ITypeNat
 {
     VmlImport.vsTrunc(Matrix256 <M, N, float> .Capacity, ref head(A), ref head(A));
     return(ref A);
 }
Exemple #2
0
 /// <summary>
 /// Populates a target matrix with the product of the operands
 /// </summary>
 /// <param name="A">The left matrix</param>
 /// <param name="B">The right matrix</param>
 /// <param name="M">The number of rows in A and C</param>
 /// <param name="N">The number of columns in B and C</param>
 /// <param name="K">The number of columns in A and rows in B</param>
 public static ref Matrix256 <N, T> gemm <N, T>(Matrix256 <N, T> A, Matrix256 <N, T> B, ref Matrix256 <N, T> X)
     where N : unmanaged, ITypeNat
     where T : unmanaged
 {
     if (typeof(T) == typeof(float))
     {
         var Z = X.As <float>();
         gemm(A.As <float>(), B.As <float>(), ref Z);
     }
     else if (typeof(T) == typeof(double))
     {
         var x = X.As <double>();
         gemm(A.As <double>(), B.As <double>(), ref x);
     }
     else if (typeof(T) == typeof(int) || typeof(T) == typeof(uint) || typeof(T) == typeof(short) || typeof(T) == typeof(ushort))
     {
         var Z = X.Convert <double>();
         X = gemm(A.Convert <double>(), B.Convert <double>(), ref Z).Convert <T>();
     }
     else if (typeof(T) == typeof(long) || typeof(T) == typeof(ulong))
     {
         var Z = X.Convert <double>();
         X = gemm(A.Convert <double>(), B.Convert <double>(), ref Z).Convert <T>();
     }
     else if (typeof(T) == typeof(byte) || typeof(T) == typeof(sbyte))
     {
         var Z = X.Convert <float>();
         X = gemm(A.Convert <float>(), B.Convert <float>(), ref Z).Convert <T>();
     }
     return(ref X);
 }
Exemple #3
0
Fichier : vml.cs Projet : 0xCM/z0
 public static ref Matrix256 <M, N, double> round <M, N>(ref Matrix256 <M, N, double> A)
     where M : unmanaged, ITypeNat
     where N : unmanaged, ITypeNat
 {
     VmlImport.vdRound(Matrix256 <M, N, double> .Capacity, ref head(A), ref head(A));
     return(ref A);
 }
Exemple #4
0
Fichier : geev.cs Projet : 0xCM/z0
        public static EigenResult <N, double> geev <N>(Matrix256 <N, double> A)
            where N : unmanaged, ITypeNat
        {
            var n        = nat32i <N>();
            var lda      = n;
            var ldvl     = n;
            var ldvr     = n;
            var wslen    = n * n;
            var exitcode = 0;
            var v        = 'V';
            var wr       = NatSpans.alloc <N, double>();
            var wi       = NatSpans.alloc <N, double>();
            var lVec     = A.Replicate();
            var rVec     = A.Replicate();

            exitcode = LAPACK.LAPACKE_dgeev(RowMajor, v, v, n, ref head(A), lda, ref wr.First, ref wi.First,
                                            ref head(lVec), ldvl, ref head(rVec), ldvr);

            if (exitcode != 0)
            {
                MklException.Throw(exitcode);
            }

            return(EigenResult.Define(FromPaired <N, double>(wr, wi), lVec, rVec));
        }
Exemple #5
0
        public static Matrix256 <N, double> Pow <N>(this Matrix256 <N, double> A, int exp)
            where N : unmanaged, ITypeNat
        {
            if (exp == 1)
            {
                return(A);
            }

            var B = A.Replicate();
            var X = Matrix.blockalloc <N, double>();

            mkl.gemm(A, B, ref X);
            if (exp == 2)
            {
                return(X);
            }

            var i = exp;

            while (--i > 2)
            {
                mkl.gemm(X, X, ref X);
            }

            return(X);
        }
Exemple #6
0
Fichier : vml.cs Projet : 0xCM/z0
        public static ref Matrix256 <M, N, double> mod <M, N>(Matrix256 <M, N, double> lhs, Matrix256 <M, N, double> rhs, ref Matrix256 <M, N, double> dst)
            where N : unmanaged, ITypeNat
            where M : unmanaged, ITypeNat

        {
            VmlImport.vdFmod(Matrix256 <M, N, float> .Capacity, ref head(lhs), ref head(rhs), ref head(dst));
            return(ref dst);
        }
Exemple #7
0
 public static ref Matrix256 <M, N, double> Mul <M, K, N>(this Matrix256 <M, K, double> A, Matrix256 <K, N, double> B, ref Matrix256 <M, N, double> X)
     where M : unmanaged, ITypeNat
     where N : unmanaged, ITypeNat
     where K : unmanaged, ITypeNat
 {
     mkl.gemm(A, B, ref X);
     return(ref X);
 }
Exemple #8
0
Fichier : vml.cs Projet : 0xCM/z0
        public static ref Matrix256 <M, N, float> mod <M, N>(Matrix256 <M, N, float> A, Matrix256 <M, N, float> B, ref Matrix256 <M, N, float> X)
            where N : unmanaged, ITypeNat
            where M : unmanaged, ITypeNat

        {
            VmlImport.vsFmod(Matrix256 <M, N, float> .Capacity, ref head(A), ref head(B), ref head(X));
            return(ref X);
        }
Exemple #9
0
        /// <summary>
        /// Computes the product of square metrices X = AB
        /// </summary>
        /// <param name="A">The left matrix</param>
        /// <param name="B">The right matrix</param>
        /// <param name="X">The target matrix</param>
        /// <param name="N">The number of columns in B and C</param>
        public static ref Matrix256 <N, double> gemm <N>(Matrix256 <N, double> A, Matrix256 <N, double> B, ref Matrix256 <N, double> X)
            where N : unmanaged, ITypeNat
        {
            var n  = nat32i <N>();
            var ld = n;

            CBLAS.cblas_dgemm(RowMajor, NoTranspose, NoTranspose, n, n, n, 1, ref head(A), ld, ref head(B), ld, 0, ref head(X), ld);
            return(ref X);
        }
Exemple #10
0
Fichier : gemv.cs Projet : 0xCM/z0
        /// <summary>
        /// Computes the matrix-vector product y = A*x;
        /// </summary>
        /// <param name="A">A source matrix of dimension MxN</param>
        /// <param name="x">A source vector of length N</param>
        /// <param name="y">A target vector of length M</param>
        /// <typeparam name="M">The row dimension type of A</typeparam>
        /// <typeparam name="N">The column dimension type of A</typeparam>
        public static ref Block256 <M, float> gemv <M, N>(Matrix256 <M, N, float> A, Block256 <N, float> x, ref Block256 <M, float> y)
            where M : unmanaged, ITypeNat
            where N : unmanaged, ITypeNat
        {
            var m   = nat32i <M>();
            var n   = nat32i <N>();
            var lda = n;

            CBLAS.cblas_sgemv(RowMajor, NoTranspose, m, n, alpha: 1f, ref head(A), lda, ref head(x), incX: 1, beta: 0, ref head(y), incY: 1);
            return(ref y);
        }
Exemple #11
0
        internal static void refmul <M, N, T>(Matrix256 <M, N, T> A, Block256 <N, T> B, Block256 <M, T> X)
            where M : unmanaged, ITypeNat
            where N : unmanaged, ITypeNat
            where T : unmanaged
        {
            var m = nat32i <M>();

            for (var i = 0; i < m; i++)
            {
                X[i] = t_dot.dot(A.GetRow(i), B);
            }
        }
Exemple #12
0
        /// <summary>
        /// Computes an LU factorization of an N-square matrix A using partial pivoting with row interchanges.
        /// </summary>
        /// <param name="A"></param>
        /// <param name="X"></param>
        /// <param name="P"></param>
        /// <typeparam name="M"></typeparam>
        /// <typeparam name="N"></typeparam>
        public static ref Matrix256 <N, double> getrf <N>(Matrix256 <N, double> A, Span <int> P, ref Matrix256 <N, double> X)
            where N : unmanaged, ITypeNat
        {
            var n   = nat32i <N>();
            var lda = n;

            A.CopyTo(ref X);
            var exit = LAPACK.LAPACKE_dgetrf(RowMajor, n, n, ref head(X), lda, ref head(P));

            checkx(exit);

            return(ref X);
        }
Exemple #13
0
        public static Matrix256 <N, T> Map <N, S, T>(this Matrix256 <N, S> A, Func <S, T> f)
            where N : unmanaged, ITypeNat
            where T : unmanaged
            where S : unmanaged
        {
            var src  = A.Unblocked;
            var dstM = Matrix.blockalloc <N, T>();
            var dst  = dstM.Unblocked;

            for (var i = 0; i < dst.Length; i++)
            {
                dst[i] = f(src[i]);
            }
            return(dstM);
        }
Exemple #14
0
        /// <summary>
        /// Populates a target matrix with the product of the operands
        /// </summary>
        /// <param name="A">The left matrix</param>
        /// <param name="B">The right matrix</param>
        /// <param name="M">The number of rows in A and C</param>
        /// <param name="N">The number of columns in B and C</param>
        /// <param name="K">The number of columns in A and rows in B</param>
        public static ref Matrix256 <M, N, double> gemm <M, K, N>(Matrix256 <M, K, double> A, Matrix256 <K, N, double> B, ref Matrix256 <M, N, double> X)
            where M : unmanaged, ITypeNat
            where K : unmanaged, ITypeNat
            where N : unmanaged, ITypeNat
        {
            var m   = nat32i <M>();
            var k   = nat32i <K>();
            var n   = nat32i <N>();
            var lda = k;
            var ldb = n;
            var ldx = n;

            CBLAS.cblas_dgemm(RowMajor, NoTranspose, NoTranspose, m, n, k, 1d, ref head(A), lda, ref head(B), ldb, 0, ref head(X), ldx);
            return(ref X);
        }
Exemple #15
0
        /// <summary>
        /// Allocates a target matrix and populates it with the product of the operands
        /// </summary>
        /// <param name="A">The left matrix</param>
        /// <param name="B">The right matrix</param>
        /// <param name="M">The number of rows in A and C</param>
        /// <param name="N">The number of columns in B and C</param>
        /// <param name="K">The number of columns in A and rows in B</param>
        public static Matrix256 <M, N, float> gemm <M, K, N>(Matrix256 <M, K, float> A, Matrix256 <K, N, float> B)
            where M : unmanaged, ITypeNat
            where K : unmanaged, ITypeNat
            where N : unmanaged, ITypeNat
        {
            var m   = nat32i <M>();
            var k   = nat32i <K>();
            var n   = nat32i <N>();
            var lda = k;
            var ldb = n;
            var ldx = n;
            var X   = Matrix.blockalloc <M, N, float>();

            CBLAS.cblas_sgemm(RowMajor, NoTranspose, NoTranspose, m, n, k, 1.0f, ref head(A), lda, ref head(B), ldb, 0, ref head(X), ldx);
            return(X);
        }
Exemple #16
0
        static ref Matrix256 <M, N, double> Mul <M, K, N>(Matrix256 <M, K, double> A, Matrix256 <K, N, double> B, ref Matrix256 <M, N, double> X)
            where M : unmanaged, ITypeNat
            where K : unmanaged, ITypeNat
            where N : unmanaged, ITypeNat
        {
            var m = nat32i <M>();
            var n = nat32i <N>();

            for (var i = 0; i < m; i++)
            {
                var row = A.GetRow(i);
                for (var j = 0; j < n; j++)
                {
                    var col = B.GetCol(j);
                    X[i, j] = Dot(row, col);
                }
            }
            return(ref X);
        }
Exemple #17
0
        /// <summary>
        /// Attempts to use the cholesky algorithm to factor a square matrix as either
        /// A = L*Transpose(L)  or A = Transpose(U)*U according to whether the tk parameter
        /// respectively specifies Lower or Upper triangulation.
        /// </summary>
        /// <param name="A">The matrix to factor and the matrix that receives the results</param>
        /// <param name="tk">The triangular classification</param>
        /// <typeparam name="N">The matrix order type</typeparam>
        public static bool potrf <N>(Matrix256 <N, double> A, TriangularKind tk = TriangularKind.Lower)
            where N : unmanaged, ITypeNat
        {
            var n   = nat32i <N>();
            var lda = n;
            var lu  = tk == TriangularKind.Lower ? 'L' : 'U';

            var exitcode = LAPACK.LAPACKE_dpotrf(RowMajor, lu, n, ref head(A), lda);

            if (exitcode > 0)
            {
                return(false);
            }
            else if (exitcode == 0)
            {
                return(true);
            }
            else
            {
                throw MklException.Define(exitcode);
            }
        }
Exemple #18
0
 static ref T head <M, N, T>(Matrix256 <M, N, T> src)
     where N : unmanaged, ITypeNat
     where M : unmanaged, ITypeNat
     where T : unmanaged
 => ref MemoryMarshal.GetReference <T>(src.Unsized);
Exemple #19
0
 public static Matrix256 <M, N, double> Mul <M, K, N>(this Matrix256 <M, K, double> A, Matrix256 <K, N, double> B)
     where M : unmanaged, ITypeNat
     where K : unmanaged, ITypeNat
     where N : unmanaged, ITypeNat
 => Matrix.blockload <M, N, double>(mkl.gemm <M, K, N>(A.Unsized, B.Unsized));
Exemple #20
0
 public static ref Matrix256 <N, float> Mul <N>(this Matrix256 <N, float> A, Matrix256 <N, float> B, ref Matrix256 <N, float> X)
     where N : unmanaged, ITypeNat
 {
     mkl.gemm(A, B, ref X);
     return(ref X);
 }
Exemple #21
0
 /// <summary>
 /// Returns true if the matrix is positive-definite, false otherwise
 /// </summary>
 /// <param name="A"></param>
 /// <typeparam name="N">The square dimenion type</typeparam>
 public static bool posdef <N>(Matrix256 <N, double> A)
     where N : unmanaged, ITypeNat
 => potrf <N>(A.Replicate());