diff --git a/Sources/Accord.Math/Matrix/Matrix.Product.Generated.cs b/Sources/Accord.Math/Matrix/Matrix.Product.Generated.cs index 4af589137..bf88c8a75 100644 --- a/Sources/Accord.Math/Matrix/Matrix.Product.Generated.cs +++ b/Sources/Accord.Math/Matrix/Matrix.Product.Generated.cs @@ -20619,7 +20619,7 @@ public static int DotAndDot(this int[] rowVector, int[][] matrix, int[] columnVe #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this int[] rowVector, int[,] matrix, int[] columnVector) + public static int DotAndDot(this int[] rowVector, int[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -20629,48 +20629,51 @@ public static unsafe int DotAndDot(this int[] rowVector, int[,] matrix, int[] co #endif int result = 0; - fixed (int* r = rowVector) - fixed (int* a = matrix) - fixed (int* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (int* r = rowVector) + fixed (int* a = matrix) + fixed (int* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + int* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -20726,7 +20729,7 @@ public static double DotAndDot(this int[] rowVector, int[][] matrix, double[] co #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this int[] rowVector, int[,] matrix, double[] columnVector) + public static double DotAndDot(this int[] rowVector, int[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -20736,48 +20739,51 @@ public static unsafe double DotAndDot(this int[] rowVector, int[,] matrix, doubl #endif double result = 0; - fixed (int* r = rowVector) - fixed (int* a = matrix) - fixed (double* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (int* r = rowVector) + fixed (int* a = matrix) + fixed (double* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + int* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -20833,7 +20839,7 @@ public static float DotAndDot(this int[] rowVector, int[][] matrix, float[] colu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this int[] rowVector, int[,] matrix, float[] columnVector) + public static float DotAndDot(this int[] rowVector, int[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -20843,48 +20849,51 @@ public static unsafe float DotAndDot(this int[] rowVector, int[,] matrix, float[ #endif float result = 0; - fixed (int* r = rowVector) - fixed (int* a = matrix) - fixed (float* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (int* r = rowVector) + fixed (int* a = matrix) + fixed (float* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + int* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -20940,7 +20949,7 @@ public static int DotAndDot(this int[] rowVector, double[][] matrix, int[] colum #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this int[] rowVector, double[,] matrix, int[] columnVector) + public static int DotAndDot(this int[] rowVector, double[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -20950,48 +20959,51 @@ public static unsafe int DotAndDot(this int[] rowVector, double[,] matrix, int[] #endif int result = 0; - fixed (int* r = rowVector) - fixed (double* a = matrix) - fixed (int* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (int* r = rowVector) + fixed (double* a = matrix) + fixed (int* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + int* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -21047,7 +21059,7 @@ public static double DotAndDot(this int[] rowVector, double[][] matrix, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this int[] rowVector, double[,] matrix, double[] columnVector) + public static double DotAndDot(this int[] rowVector, double[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -21057,48 +21069,51 @@ public static unsafe double DotAndDot(this int[] rowVector, double[,] matrix, do #endif double result = 0; - fixed (int* r = rowVector) - fixed (double* a = matrix) - fixed (double* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (int* r = rowVector) + fixed (double* a = matrix) + fixed (double* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + int* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -21154,7 +21169,7 @@ public static float DotAndDot(this int[] rowVector, double[][] matrix, float[] c #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this int[] rowVector, double[,] matrix, float[] columnVector) + public static float DotAndDot(this int[] rowVector, double[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -21164,48 +21179,51 @@ public static unsafe float DotAndDot(this int[] rowVector, double[,] matrix, flo #endif float result = 0; - fixed (int* r = rowVector) - fixed (double* a = matrix) - fixed (float* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (int* r = rowVector) + fixed (double* a = matrix) + fixed (float* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + int* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -21261,7 +21279,7 @@ public static double DotAndDot(this int[] rowVector, float[][] matrix, int[] col #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this int[] rowVector, float[,] matrix, int[] columnVector) + public static double DotAndDot(this int[] rowVector, float[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -21271,48 +21289,51 @@ public static unsafe double DotAndDot(this int[] rowVector, float[,] matrix, int #endif double result = 0; - fixed (int* r = rowVector) - fixed (float* a = matrix) - fixed (int* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (int* r = rowVector) + fixed (float* a = matrix) + fixed (int* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + int* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -21368,7 +21389,7 @@ public static double DotAndDot(this int[] rowVector, float[][] matrix, float[] c #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this int[] rowVector, float[,] matrix, float[] columnVector) + public static double DotAndDot(this int[] rowVector, float[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -21378,48 +21399,51 @@ public static unsafe double DotAndDot(this int[] rowVector, float[,] matrix, flo #endif double result = 0; - fixed (int* r = rowVector) - fixed (float* a = matrix) - fixed (float* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (int* r = rowVector) + fixed (float* a = matrix) + fixed (float* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + int* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -21475,7 +21499,7 @@ public static double DotAndDot(this int[] rowVector, float[][] matrix, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this int[] rowVector, float[,] matrix, double[] columnVector) + public static double DotAndDot(this int[] rowVector, float[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -21485,48 +21509,51 @@ public static unsafe double DotAndDot(this int[] rowVector, float[,] matrix, dou #endif double result = 0; - fixed (int* r = rowVector) - fixed (float* a = matrix) - fixed (double* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (int* r = rowVector) + fixed (float* a = matrix) + fixed (double* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + int* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -21582,7 +21609,7 @@ public static double DotAndDot(this double[] rowVector, double[][] matrix, doubl #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this double[] rowVector, double[,] matrix, double[] columnVector) + public static double DotAndDot(this double[] rowVector, double[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -21592,48 +21619,51 @@ public static unsafe double DotAndDot(this double[] rowVector, double[,] matrix, #endif double result = 0; - fixed (double* r = rowVector) - fixed (double* a = matrix) - fixed (double* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (double* r = rowVector) + fixed (double* a = matrix) + fixed (double* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + double* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -21689,7 +21719,7 @@ public static int DotAndDot(this double[] rowVector, double[][] matrix, int[] co #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this double[] rowVector, double[,] matrix, int[] columnVector) + public static int DotAndDot(this double[] rowVector, double[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -21699,48 +21729,51 @@ public static unsafe int DotAndDot(this double[] rowVector, double[,] matrix, in #endif int result = 0; - fixed (double* r = rowVector) - fixed (double* a = matrix) - fixed (int* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (double* r = rowVector) + fixed (double* a = matrix) + fixed (int* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + double* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -21796,7 +21829,7 @@ public static float DotAndDot(this double[] rowVector, double[][] matrix, float[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this double[] rowVector, double[,] matrix, float[] columnVector) + public static float DotAndDot(this double[] rowVector, double[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -21806,48 +21839,51 @@ public static unsafe float DotAndDot(this double[] rowVector, double[,] matrix, #endif float result = 0; - fixed (double* r = rowVector) - fixed (double* a = matrix) - fixed (float* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (double* r = rowVector) + fixed (double* a = matrix) + fixed (float* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + double* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -21903,7 +21939,7 @@ public static double DotAndDot(this double[] rowVector, int[][] matrix, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this double[] rowVector, int[,] matrix, double[] columnVector) + public static double DotAndDot(this double[] rowVector, int[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -21913,48 +21949,51 @@ public static unsafe double DotAndDot(this double[] rowVector, int[,] matrix, do #endif double result = 0; - fixed (double* r = rowVector) - fixed (int* a = matrix) - fixed (double* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (double* r = rowVector) + fixed (int* a = matrix) + fixed (double* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + double* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -22010,7 +22049,7 @@ public static int DotAndDot(this double[] rowVector, int[][] matrix, int[] colum #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this double[] rowVector, int[,] matrix, int[] columnVector) + public static int DotAndDot(this double[] rowVector, int[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -22020,48 +22059,51 @@ public static unsafe int DotAndDot(this double[] rowVector, int[,] matrix, int[] #endif int result = 0; - fixed (double* r = rowVector) - fixed (int* a = matrix) - fixed (int* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (double* r = rowVector) + fixed (int* a = matrix) + fixed (int* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + double* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -22117,7 +22159,7 @@ public static float DotAndDot(this double[] rowVector, int[][] matrix, float[] c #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this double[] rowVector, int[,] matrix, float[] columnVector) + public static float DotAndDot(this double[] rowVector, int[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -22127,48 +22169,51 @@ public static unsafe float DotAndDot(this double[] rowVector, int[,] matrix, flo #endif float result = 0; - fixed (double* r = rowVector) - fixed (int* a = matrix) - fixed (float* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (double* r = rowVector) + fixed (int* a = matrix) + fixed (float* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + double* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -22224,7 +22269,7 @@ public static double DotAndDot(this double[] rowVector, float[][] matrix, double #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this double[] rowVector, float[,] matrix, double[] columnVector) + public static double DotAndDot(this double[] rowVector, float[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -22234,48 +22279,51 @@ public static unsafe double DotAndDot(this double[] rowVector, float[,] matrix, #endif double result = 0; - fixed (double* r = rowVector) - fixed (float* a = matrix) - fixed (double* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (double* r = rowVector) + fixed (float* a = matrix) + fixed (double* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + double* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -22331,7 +22379,7 @@ public static double DotAndDot(this double[] rowVector, float[][] matrix, float[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this double[] rowVector, float[,] matrix, float[] columnVector) + public static double DotAndDot(this double[] rowVector, float[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -22341,48 +22389,51 @@ public static unsafe double DotAndDot(this double[] rowVector, float[,] matrix, #endif double result = 0; - fixed (double* r = rowVector) - fixed (float* a = matrix) - fixed (float* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (double* r = rowVector) + fixed (float* a = matrix) + fixed (float* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + double* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -22438,7 +22489,7 @@ public static double DotAndDot(this double[] rowVector, float[][] matrix, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this double[] rowVector, float[,] matrix, int[] columnVector) + public static double DotAndDot(this double[] rowVector, float[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -22448,48 +22499,51 @@ public static unsafe double DotAndDot(this double[] rowVector, float[,] matrix, #endif double result = 0; - fixed (double* r = rowVector) - fixed (float* a = matrix) - fixed (int* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (double* r = rowVector) + fixed (float* a = matrix) + fixed (int* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + double* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -22545,7 +22599,7 @@ public static double DotAndDot(this float[] rowVector, float[][] matrix, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this float[] rowVector, float[,] matrix, float[] columnVector) + public static double DotAndDot(this float[] rowVector, float[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -22555,48 +22609,51 @@ public static unsafe double DotAndDot(this float[] rowVector, float[,] matrix, f #endif double result = 0; - fixed (float* r = rowVector) - fixed (float* a = matrix) - fixed (float* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (float* r = rowVector) + fixed (float* a = matrix) + fixed (float* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + float* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -22652,7 +22709,7 @@ public static double DotAndDot(this float[] rowVector, float[][] matrix, double[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this float[] rowVector, float[,] matrix, double[] columnVector) + public static double DotAndDot(this float[] rowVector, float[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -22662,48 +22719,51 @@ public static unsafe double DotAndDot(this float[] rowVector, float[,] matrix, d #endif double result = 0; - fixed (float* r = rowVector) - fixed (float* a = matrix) - fixed (double* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (float* r = rowVector) + fixed (float* a = matrix) + fixed (double* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + float* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -22759,7 +22819,7 @@ public static double DotAndDot(this float[] rowVector, float[][] matrix, int[] c #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this float[] rowVector, float[,] matrix, int[] columnVector) + public static double DotAndDot(this float[] rowVector, float[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -22769,48 +22829,51 @@ public static unsafe double DotAndDot(this float[] rowVector, float[,] matrix, i #endif double result = 0; - fixed (float* r = rowVector) - fixed (float* a = matrix) - fixed (int* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (float* r = rowVector) + fixed (float* a = matrix) + fixed (int* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + float* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -22866,7 +22929,7 @@ public static double DotAndDot(this float[] rowVector, double[][] matrix, float[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this float[] rowVector, double[,] matrix, float[] columnVector) + public static double DotAndDot(this float[] rowVector, double[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -22876,155 +22939,51 @@ public static unsafe double DotAndDot(this float[] rowVector, double[,] matrix, #endif double result = 0; - fixed (float* r = rowVector) - fixed (double* a = matrix) - fixed (float* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } - - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); - - result += (double)((double)(*pr++) * sum); - } - } - - return result; - } + unsafe + { + fixed (float* r = rowVector) + fixed (double* a = matrix) + fixed (float* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + float* pr = r; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* pc = c; + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); - /// - /// Computes the product a*B*c of a row vector a, - /// a square matrix B and a column vector c. - /// - /// - /// The left vector a. - /// The square matrix B. - /// The column vector c. - /// - /// The product a*B*c of the given vector a, - /// matrix B and vector c. - /// -#if NET45 || NET46 || NET462 || NETSTANDARD - [MethodImpl(MethodImplOptions.AggressiveInlining)] -#endif - public static double DotAndDot(this float[] rowVector, double[][] matrix, double[] columnVector) - { -#if DEBUG - if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) - throw new DimensionMismatchException(); -#endif - double sum = 0; + result += (double)((double)(*pr++) * sum); + } + } + } - for (int i = 0; i < rowVector.Length; i++) - { - double s = 0; - for (int j = 0; j < columnVector.Length; j++) - s += (double)((double)matrix[i][j] * (double)columnVector[j]); - sum += (double)((double)rowVector[i] * (double)s); - } - - return sum; - } - - /// - /// Computes the product a*B*c of a row vector a, - /// a square matrix B and a column vector c. - /// - /// - /// The left vector a. - /// The square matrix B. - /// The column vector c. - /// - /// The product a*B*c of the given vector a, - /// matrix B and vector c. - /// -#if NET45 || NET46 || NET462 || NETSTANDARD - [MethodImpl(MethodImplOptions.AggressiveInlining)] -#endif - public static unsafe double DotAndDot(this float[] rowVector, double[,] matrix, double[] columnVector) - { - int cols = matrix.Columns(); - int rows = matrix.Rows(); -#if DEBUG - if (rowVector.Length != rows || cols != columnVector.Length) - throw new DimensionMismatchException(); -#endif - double result = 0; - - fixed (float* r = rowVector) - fixed (double* a = matrix) - fixed (double* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } - - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); - - result += (double)((double)(*pr++) * sum); - } - } - return result; } @@ -23046,7 +23005,7 @@ public static unsafe double DotAndDot(this float[] rowVector, double[,] matrix, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static double DotAndDot(this float[] rowVector, double[][] matrix, int[] columnVector) + public static double DotAndDot(this float[] rowVector, double[][] matrix, double[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) @@ -23080,7 +23039,7 @@ public static double DotAndDot(this float[] rowVector, double[][] matrix, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this float[] rowVector, double[,] matrix, int[] columnVector) + public static double DotAndDot(this float[] rowVector, double[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -23090,48 +23049,51 @@ public static unsafe double DotAndDot(this float[] rowVector, double[,] matrix, #endif double result = 0; - fixed (float* r = rowVector) - fixed (double* a = matrix) - fixed (int* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); + unsafe + { + fixed (float* r = rowVector) + fixed (double* a = matrix) + fixed (double* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + float* pr = r; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* pc = c; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); - - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -23153,7 +23115,7 @@ public static unsafe double DotAndDot(this float[] rowVector, double[,] matrix, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static double DotAndDot(this float[] rowVector, int[][] matrix, float[] columnVector) + public static double DotAndDot(this float[] rowVector, double[][] matrix, int[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) @@ -23187,7 +23149,7 @@ public static double DotAndDot(this float[] rowVector, int[][] matrix, float[] c #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this float[] rowVector, int[,] matrix, float[] columnVector) + public static double DotAndDot(this float[] rowVector, double[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -23197,48 +23159,51 @@ public static unsafe double DotAndDot(this float[] rowVector, int[,] matrix, flo #endif double result = 0; - fixed (float* r = rowVector) - fixed (int* a = matrix) - fixed (float* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (float* r = rowVector) + fixed (double* a = matrix) + fixed (int* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + float* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -23260,7 +23225,7 @@ public static unsafe double DotAndDot(this float[] rowVector, int[,] matrix, flo #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static double DotAndDot(this float[] rowVector, int[][] matrix, int[] columnVector) + public static double DotAndDot(this float[] rowVector, int[][] matrix, float[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) @@ -23294,7 +23259,7 @@ public static double DotAndDot(this float[] rowVector, int[][] matrix, int[] col #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this float[] rowVector, int[,] matrix, int[] columnVector) + public static double DotAndDot(this float[] rowVector, int[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -23304,48 +23269,51 @@ public static unsafe double DotAndDot(this float[] rowVector, int[,] matrix, int #endif double result = 0; - fixed (float* r = rowVector) - fixed (int* a = matrix) - fixed (int* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (float* r = rowVector) + fixed (int* a = matrix) + fixed (float* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + float* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -23367,7 +23335,7 @@ public static unsafe double DotAndDot(this float[] rowVector, int[,] matrix, int #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static double DotAndDot(this float[] rowVector, int[][] matrix, double[] columnVector) + public static double DotAndDot(this float[] rowVector, int[][] matrix, int[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) @@ -23401,7 +23369,7 @@ public static double DotAndDot(this float[] rowVector, int[][] matrix, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this float[] rowVector, int[,] matrix, double[] columnVector) + public static double DotAndDot(this float[] rowVector, int[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -23411,48 +23379,51 @@ public static unsafe double DotAndDot(this float[] rowVector, int[,] matrix, dou #endif double result = 0; - fixed (float* r = rowVector) - fixed (int* a = matrix) - fixed (double* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (float* r = rowVector) + fixed (int* a = matrix) + fixed (int* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + float* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -23474,20 +23445,20 @@ public static unsafe double DotAndDot(this float[] rowVector, int[,] matrix, dou #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static long DotAndDot(this long[] rowVector, long[][] matrix, long[] columnVector) + public static double DotAndDot(this float[] rowVector, int[][] matrix, double[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - long sum = 0; + double sum = 0; for (int i = 0; i < rowVector.Length; i++) { - long s = 0; + double s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (long)((long)matrix[i][j] * (long)columnVector[j]); - sum += (long)((long)rowVector[i] * (long)s); + s += (double)((double)matrix[i][j] * (double)columnVector[j]); + sum += (double)((double)rowVector[i] * (double)s); } return sum; @@ -23508,7 +23479,7 @@ public static long DotAndDot(this long[] rowVector, long[][] matrix, long[] colu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long DotAndDot(this long[] rowVector, long[,] matrix, long[] columnVector) + public static double DotAndDot(this float[] rowVector, int[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -23516,50 +23487,53 @@ public static unsafe long DotAndDot(this long[] rowVector, long[,] matrix, long[ if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - long result = 0; - - fixed (long* r = rowVector) - fixed (long* a = matrix) - fixed (long* c = columnVector) - { - long* pa1 = a; - long* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - long sum1 = 0, sum2 = 0; - long* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (long)((long)(*pa1++) * (long)(*pc)); - sum2 += (long)((long)(*pa2++) * (long)(*pc)); - pc++; - } - - result += (long)((long)(*pr++) * sum1); - result += (long)((long)(*pr++) * sum2); + double result = 0; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (float* r = rowVector) + fixed (int* a = matrix) + fixed (double* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + float* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - long sum = 0; - long* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (long)((long)(*pa1++) * (long)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (long)((long)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -23581,20 +23555,20 @@ public static unsafe long DotAndDot(this long[] rowVector, long[,] matrix, long[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static double DotAndDot(this long[] rowVector, long[][] matrix, double[] columnVector) + public static long DotAndDot(this long[] rowVector, long[][] matrix, long[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - double sum = 0; + long sum = 0; for (int i = 0; i < rowVector.Length; i++) { - double s = 0; + long s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (double)((double)matrix[i][j] * (double)columnVector[j]); - sum += (double)((double)rowVector[i] * (double)s); + s += (long)((long)matrix[i][j] * (long)columnVector[j]); + sum += (long)((long)rowVector[i] * (long)s); } return sum; @@ -23615,7 +23589,7 @@ public static double DotAndDot(this long[] rowVector, long[][] matrix, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this long[] rowVector, long[,] matrix, double[] columnVector) + public static long DotAndDot(this long[] rowVector, long[,] matrix, long[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -23623,50 +23597,53 @@ public static unsafe double DotAndDot(this long[] rowVector, long[,] matrix, dou if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - double result = 0; - - fixed (long* r = rowVector) - fixed (long* a = matrix) - fixed (double* c = columnVector) - { - long* pa1 = a; - long* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); + long result = 0; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (long* r = rowVector) + fixed (long* a = matrix) + fixed (long* c = columnVector) + { + long* pa1 = a; + long* pa2 = a + cols; + long* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + long sum1 = 0, sum2 = 0; + long* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (long)((long)(*pa1++) * (long)(*pc)); + sum2 += (long)((long)(*pa2++) * (long)(*pc)); + pc++; + } + + result += (long)((long)(*pr++) * sum1); + result += (long)((long)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + long sum = 0; + long* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (long)((long)(*pa1++) * (long)(*pc++)); + result += (long)((long)(*pr++) * sum); + } + } + } + return result; } @@ -23688,20 +23665,20 @@ public static unsafe double DotAndDot(this long[] rowVector, long[,] matrix, dou #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static int DotAndDot(this long[] rowVector, long[][] matrix, int[] columnVector) + public static double DotAndDot(this long[] rowVector, long[][] matrix, double[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - int sum = 0; + double sum = 0; for (int i = 0; i < rowVector.Length; i++) { - int s = 0; + double s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (int)((int)matrix[i][j] * (int)columnVector[j]); - sum += (int)((int)rowVector[i] * (int)s); + s += (double)((double)matrix[i][j] * (double)columnVector[j]); + sum += (double)((double)rowVector[i] * (double)s); } return sum; @@ -23722,7 +23699,7 @@ public static int DotAndDot(this long[] rowVector, long[][] matrix, int[] column #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this long[] rowVector, long[,] matrix, int[] columnVector) + public static double DotAndDot(this long[] rowVector, long[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -23730,50 +23707,53 @@ public static unsafe int DotAndDot(this long[] rowVector, long[,] matrix, int[] if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - int result = 0; - - fixed (long* r = rowVector) - fixed (long* a = matrix) - fixed (int* c = columnVector) - { - long* pa1 = a; - long* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); + double result = 0; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (long* r = rowVector) + fixed (long* a = matrix) + fixed (double* c = columnVector) + { + long* pa1 = a; + long* pa2 = a + cols; + long* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -23795,20 +23775,20 @@ public static unsafe int DotAndDot(this long[] rowVector, long[,] matrix, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static float DotAndDot(this long[] rowVector, long[][] matrix, float[] columnVector) + public static int DotAndDot(this long[] rowVector, long[][] matrix, int[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - float sum = 0; + int sum = 0; for (int i = 0; i < rowVector.Length; i++) { - float s = 0; + int s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (float)((float)matrix[i][j] * (float)columnVector[j]); - sum += (float)((float)rowVector[i] * (float)s); + s += (int)((int)matrix[i][j] * (int)columnVector[j]); + sum += (int)((int)rowVector[i] * (int)s); } return sum; @@ -23829,7 +23809,7 @@ public static float DotAndDot(this long[] rowVector, long[][] matrix, float[] co #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this long[] rowVector, long[,] matrix, float[] columnVector) + public static int DotAndDot(this long[] rowVector, long[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -23837,50 +23817,53 @@ public static unsafe float DotAndDot(this long[] rowVector, long[,] matrix, floa if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - float result = 0; - - fixed (long* r = rowVector) - fixed (long* a = matrix) - fixed (float* c = columnVector) - { - long* pa1 = a; - long* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); + int result = 0; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (long* r = rowVector) + fixed (long* a = matrix) + fixed (int* c = columnVector) + { + long* pa1 = a; + long* pa2 = a + cols; + long* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -23902,20 +23885,20 @@ public static unsafe float DotAndDot(this long[] rowVector, long[,] matrix, floa #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static long DotAndDot(this long[] rowVector, double[][] matrix, long[] columnVector) + public static float DotAndDot(this long[] rowVector, long[][] matrix, float[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - long sum = 0; + float sum = 0; for (int i = 0; i < rowVector.Length; i++) { - long s = 0; + float s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (long)((long)matrix[i][j] * (long)columnVector[j]); - sum += (long)((long)rowVector[i] * (long)s); + s += (float)((float)matrix[i][j] * (float)columnVector[j]); + sum += (float)((float)rowVector[i] * (float)s); } return sum; @@ -23936,7 +23919,7 @@ public static long DotAndDot(this long[] rowVector, double[][] matrix, long[] co #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long DotAndDot(this long[] rowVector, double[,] matrix, long[] columnVector) + public static float DotAndDot(this long[] rowVector, long[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -23944,50 +23927,53 @@ public static unsafe long DotAndDot(this long[] rowVector, double[,] matrix, lon if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - long result = 0; - - fixed (long* r = rowVector) - fixed (double* a = matrix) - fixed (long* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - long sum1 = 0, sum2 = 0; - long* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (long)((long)(*pa1++) * (long)(*pc)); - sum2 += (long)((long)(*pa2++) * (long)(*pc)); - pc++; - } - - result += (long)((long)(*pr++) * sum1); - result += (long)((long)(*pr++) * sum2); + float result = 0; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (long* r = rowVector) + fixed (long* a = matrix) + fixed (float* c = columnVector) + { + long* pa1 = a; + long* pa2 = a + cols; + long* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - long sum = 0; - long* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (long)((long)(*pa1++) * (long)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (long)((long)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -24009,20 +23995,20 @@ public static unsafe long DotAndDot(this long[] rowVector, double[,] matrix, lon #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static double DotAndDot(this long[] rowVector, double[][] matrix, double[] columnVector) + public static long DotAndDot(this long[] rowVector, double[][] matrix, long[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - double sum = 0; + long sum = 0; for (int i = 0; i < rowVector.Length; i++) { - double s = 0; + long s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (double)((double)matrix[i][j] * (double)columnVector[j]); - sum += (double)((double)rowVector[i] * (double)s); + s += (long)((long)matrix[i][j] * (long)columnVector[j]); + sum += (long)((long)rowVector[i] * (long)s); } return sum; @@ -24043,7 +24029,7 @@ public static double DotAndDot(this long[] rowVector, double[][] matrix, double[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this long[] rowVector, double[,] matrix, double[] columnVector) + public static long DotAndDot(this long[] rowVector, double[,] matrix, long[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -24051,50 +24037,163 @@ public static unsafe double DotAndDot(this long[] rowVector, double[,] matrix, d if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - double result = 0; + long result = 0; - fixed (long* r = rowVector) - fixed (double* a = matrix) - fixed (double* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - long* pr = r; + unsafe + { + fixed (long* r = rowVector) + fixed (double* a = matrix) + fixed (long* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + long* pr = r; - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + long sum1 = 0, sum2 = 0; + long* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (long)((long)(*pa1++) * (long)(*pc)); + sum2 += (long)((long)(*pa2++) * (long)(*pc)); + pc++; + } + + result += (long)((long)(*pr++) * sum1); + result += (long)((long)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + long sum = 0; + long* pc = c; - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); + for (int j = 0; j < cols; j++) + sum += (long)((long)(*pa1++) * (long)(*pc++)); - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + result += (long)((long)(*pr++) * sum); + } + } + } + + return result; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); - result += (double)((double)(*pr++) * sum); - } + + /// + /// Computes the product a*B*c of a row vector a, + /// a square matrix B and a column vector c. + /// + /// + /// The left vector a. + /// The square matrix B. + /// The column vector c. + /// + /// The product a*B*c of the given vector a, + /// matrix B and vector c. + /// +#if NET45 || NET46 || NET462 || NETSTANDARD + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif + public static double DotAndDot(this long[] rowVector, double[][] matrix, double[] columnVector) + { +#if DEBUG + if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) + throw new DimensionMismatchException(); +#endif + double sum = 0; + + for (int i = 0; i < rowVector.Length; i++) + { + double s = 0; + for (int j = 0; j < columnVector.Length; j++) + s += (double)((double)matrix[i][j] * (double)columnVector[j]); + sum += (double)((double)rowVector[i] * (double)s); } + return sum; + } + + /// + /// Computes the product a*B*c of a row vector a, + /// a square matrix B and a column vector c. + /// + /// + /// The left vector a. + /// The square matrix B. + /// The column vector c. + /// + /// The product a*B*c of the given vector a, + /// matrix B and vector c. + /// +#if NET45 || NET46 || NET462 || NETSTANDARD + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif + public static double DotAndDot(this long[] rowVector, double[,] matrix, double[] columnVector) + { + int cols = matrix.Columns(); + int rows = matrix.Rows(); +#if DEBUG + if (rowVector.Length != rows || cols != columnVector.Length) + throw new DimensionMismatchException(); +#endif + double result = 0; + + unsafe + { + fixed (long* r = rowVector) + fixed (double* a = matrix) + fixed (double* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + long* pr = r; + + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } + + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -24150,7 +24249,7 @@ public static int DotAndDot(this long[] rowVector, double[][] matrix, int[] colu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this long[] rowVector, double[,] matrix, int[] columnVector) + public static int DotAndDot(this long[] rowVector, double[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -24160,48 +24259,51 @@ public static unsafe int DotAndDot(this long[] rowVector, double[,] matrix, int[ #endif int result = 0; - fixed (long* r = rowVector) - fixed (double* a = matrix) - fixed (int* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (long* r = rowVector) + fixed (double* a = matrix) + fixed (int* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + long* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -24257,7 +24359,7 @@ public static float DotAndDot(this long[] rowVector, double[][] matrix, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this long[] rowVector, double[,] matrix, float[] columnVector) + public static float DotAndDot(this long[] rowVector, double[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -24267,48 +24369,51 @@ public static unsafe float DotAndDot(this long[] rowVector, double[,] matrix, fl #endif float result = 0; - fixed (long* r = rowVector) - fixed (double* a = matrix) - fixed (float* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (long* r = rowVector) + fixed (double* a = matrix) + fixed (float* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + long* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -24364,7 +24469,7 @@ public static long DotAndDot(this long[] rowVector, int[][] matrix, long[] colum #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long DotAndDot(this long[] rowVector, int[,] matrix, long[] columnVector) + public static long DotAndDot(this long[] rowVector, int[,] matrix, long[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -24374,48 +24479,51 @@ public static unsafe long DotAndDot(this long[] rowVector, int[,] matrix, long[] #endif long result = 0; - fixed (long* r = rowVector) - fixed (int* a = matrix) - fixed (long* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - long sum1 = 0, sum2 = 0; - long* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (long)((long)(*pa1++) * (long)(*pc)); - sum2 += (long)((long)(*pa2++) * (long)(*pc)); - pc++; - } - - result += (long)((long)(*pr++) * sum1); - result += (long)((long)(*pr++) * sum2); + unsafe + { + fixed (long* r = rowVector) + fixed (int* a = matrix) + fixed (long* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + long* pr = r; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + long sum1 = 0, sum2 = 0; + long* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (long)((long)(*pa1++) * (long)(*pc)); + sum2 += (long)((long)(*pa2++) * (long)(*pc)); + pc++; + } + + result += (long)((long)(*pr++) * sum1); + result += (long)((long)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - long sum = 0; - long* pc = c; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + long sum = 0; + long* pc = c; - for (int j = 0; j < cols; j++) - sum += (long)((long)(*pa1++) * (long)(*pc++)); - - result += (long)((long)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (long)((long)(*pa1++) * (long)(*pc++)); + result += (long)((long)(*pr++) * sum); + } + } + } + return result; } @@ -24471,7 +24579,7 @@ public static int DotAndDot(this long[] rowVector, int[][] matrix, int[] columnV #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this long[] rowVector, int[,] matrix, int[] columnVector) + public static int DotAndDot(this long[] rowVector, int[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -24481,48 +24589,51 @@ public static unsafe int DotAndDot(this long[] rowVector, int[,] matrix, int[] c #endif int result = 0; - fixed (long* r = rowVector) - fixed (int* a = matrix) - fixed (int* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (long* r = rowVector) + fixed (int* a = matrix) + fixed (int* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + long* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -24578,7 +24689,7 @@ public static double DotAndDot(this long[] rowVector, int[][] matrix, double[] c #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this long[] rowVector, int[,] matrix, double[] columnVector) + public static double DotAndDot(this long[] rowVector, int[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -24588,48 +24699,51 @@ public static unsafe double DotAndDot(this long[] rowVector, int[,] matrix, doub #endif double result = 0; - fixed (long* r = rowVector) - fixed (int* a = matrix) - fixed (double* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (long* r = rowVector) + fixed (int* a = matrix) + fixed (double* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + long* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -24685,7 +24799,7 @@ public static float DotAndDot(this long[] rowVector, int[][] matrix, float[] col #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this long[] rowVector, int[,] matrix, float[] columnVector) + public static float DotAndDot(this long[] rowVector, int[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -24695,48 +24809,51 @@ public static unsafe float DotAndDot(this long[] rowVector, int[,] matrix, float #endif float result = 0; - fixed (long* r = rowVector) - fixed (int* a = matrix) - fixed (float* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (long* r = rowVector) + fixed (int* a = matrix) + fixed (float* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + long* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -24792,7 +24909,7 @@ public static double DotAndDot(this long[] rowVector, float[][] matrix, long[] c #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this long[] rowVector, float[,] matrix, long[] columnVector) + public static double DotAndDot(this long[] rowVector, float[,] matrix, long[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -24802,48 +24919,51 @@ public static unsafe double DotAndDot(this long[] rowVector, float[,] matrix, lo #endif double result = 0; - fixed (long* r = rowVector) - fixed (float* a = matrix) - fixed (long* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - long* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (long* r = rowVector) + fixed (float* a = matrix) + fixed (long* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + long* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - long* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + long* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + long* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -24899,7 +25019,7 @@ public static double DotAndDot(this long[] rowVector, float[][] matrix, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this long[] rowVector, float[,] matrix, float[] columnVector) + public static double DotAndDot(this long[] rowVector, float[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -24909,48 +25029,51 @@ public static unsafe double DotAndDot(this long[] rowVector, float[,] matrix, fl #endif double result = 0; - fixed (long* r = rowVector) - fixed (float* a = matrix) - fixed (float* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); + unsafe + { + fixed (long* r = rowVector) + fixed (float* a = matrix) + fixed (float* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + long* pr = r; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* pc = c; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* pc = c; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); - - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -25006,7 +25129,7 @@ public static double DotAndDot(this long[] rowVector, float[][] matrix, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this long[] rowVector, float[,] matrix, double[] columnVector) + public static double DotAndDot(this long[] rowVector, float[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -25016,48 +25139,51 @@ public static unsafe double DotAndDot(this long[] rowVector, float[,] matrix, do #endif double result = 0; - fixed (long* r = rowVector) - fixed (float* a = matrix) - fixed (double* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (long* r = rowVector) + fixed (float* a = matrix) + fixed (double* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + long* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -25113,7 +25239,7 @@ public static double DotAndDot(this long[] rowVector, float[][] matrix, int[] co #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this long[] rowVector, float[,] matrix, int[] columnVector) + public static double DotAndDot(this long[] rowVector, float[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -25123,48 +25249,51 @@ public static unsafe double DotAndDot(this long[] rowVector, float[,] matrix, in #endif double result = 0; - fixed (long* r = rowVector) - fixed (float* a = matrix) - fixed (int* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (long* r = rowVector) + fixed (float* a = matrix) + fixed (int* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + long* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -25220,7 +25349,7 @@ public static decimal DotAndDot(this decimal[] rowVector, decimal[][] matrix, de #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal DotAndDot(this decimal[] rowVector, decimal[,] matrix, decimal[] columnVector) + public static decimal DotAndDot(this decimal[] rowVector, decimal[,] matrix, decimal[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -25230,48 +25359,51 @@ public static unsafe decimal DotAndDot(this decimal[] rowVector, decimal[,] matr #endif decimal result = 0; - fixed (decimal* r = rowVector) - fixed (decimal* a = matrix) - fixed (decimal* c = columnVector) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - decimal sum1 = 0, sum2 = 0; - decimal* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (decimal)((decimal)(*pa1++) * (decimal)(*pc)); - sum2 += (decimal)((decimal)(*pa2++) * (decimal)(*pc)); - pc++; - } - - result += (decimal)((decimal)(*pr++) * sum1); - result += (decimal)((decimal)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (decimal* a = matrix) + fixed (decimal* c = columnVector) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - decimal sum = 0; - decimal* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + decimal sum1 = 0, sum2 = 0; + decimal* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (decimal)((decimal)(*pa1++) * (decimal)(*pc)); + sum2 += (decimal)((decimal)(*pa2++) * (decimal)(*pc)); + pc++; + } + + result += (decimal)((decimal)(*pr++) * sum1); + result += (decimal)((decimal)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (decimal)((decimal)(*pa1++) * (decimal)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + decimal sum = 0; + decimal* pc = c; - result += (decimal)((decimal)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (decimal)((decimal)(*pa1++) * (decimal)(*pc++)); + result += (decimal)((decimal)(*pr++) * sum); + } + } + } + return result; } @@ -25327,7 +25459,7 @@ public static double DotAndDot(this decimal[] rowVector, decimal[][] matrix, dou #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this decimal[] rowVector, decimal[,] matrix, double[] columnVector) + public static double DotAndDot(this decimal[] rowVector, decimal[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -25337,48 +25469,51 @@ public static unsafe double DotAndDot(this decimal[] rowVector, decimal[,] matri #endif double result = 0; - fixed (decimal* r = rowVector) - fixed (decimal* a = matrix) - fixed (double* c = columnVector) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (decimal* a = matrix) + fixed (double* c = columnVector) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -25434,7 +25569,7 @@ public static int DotAndDot(this decimal[] rowVector, decimal[][] matrix, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this decimal[] rowVector, decimal[,] matrix, int[] columnVector) + public static int DotAndDot(this decimal[] rowVector, decimal[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -25444,48 +25579,51 @@ public static unsafe int DotAndDot(this decimal[] rowVector, decimal[,] matrix, #endif int result = 0; - fixed (decimal* r = rowVector) - fixed (decimal* a = matrix) - fixed (int* c = columnVector) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (decimal* a = matrix) + fixed (int* c = columnVector) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -25541,7 +25679,7 @@ public static float DotAndDot(this decimal[] rowVector, decimal[][] matrix, floa #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this decimal[] rowVector, decimal[,] matrix, float[] columnVector) + public static float DotAndDot(this decimal[] rowVector, decimal[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -25551,48 +25689,51 @@ public static unsafe float DotAndDot(this decimal[] rowVector, decimal[,] matrix #endif float result = 0; - fixed (decimal* r = rowVector) - fixed (decimal* a = matrix) - fixed (float* c = columnVector) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (decimal* a = matrix) + fixed (float* c = columnVector) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -25648,7 +25789,7 @@ public static decimal DotAndDot(this decimal[] rowVector, double[][] matrix, dec #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal DotAndDot(this decimal[] rowVector, double[,] matrix, decimal[] columnVector) + public static decimal DotAndDot(this decimal[] rowVector, double[,] matrix, decimal[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -25658,48 +25799,51 @@ public static unsafe decimal DotAndDot(this decimal[] rowVector, double[,] matri #endif decimal result = 0; - fixed (decimal* r = rowVector) - fixed (double* a = matrix) - fixed (decimal* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - decimal sum1 = 0, sum2 = 0; - decimal* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (decimal)((decimal)(*pa1++) * (decimal)(*pc)); - sum2 += (decimal)((decimal)(*pa2++) * (decimal)(*pc)); - pc++; - } - - result += (decimal)((decimal)(*pr++) * sum1); - result += (decimal)((decimal)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (double* a = matrix) + fixed (decimal* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - decimal sum = 0; - decimal* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + decimal sum1 = 0, sum2 = 0; + decimal* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (decimal)((decimal)(*pa1++) * (decimal)(*pc)); + sum2 += (decimal)((decimal)(*pa2++) * (decimal)(*pc)); + pc++; + } + + result += (decimal)((decimal)(*pr++) * sum1); + result += (decimal)((decimal)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (decimal)((decimal)(*pa1++) * (decimal)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + decimal sum = 0; + decimal* pc = c; - result += (decimal)((decimal)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (decimal)((decimal)(*pa1++) * (decimal)(*pc++)); + result += (decimal)((decimal)(*pr++) * sum); + } + } + } + return result; } @@ -25755,7 +25899,7 @@ public static double DotAndDot(this decimal[] rowVector, double[][] matrix, doub #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this decimal[] rowVector, double[,] matrix, double[] columnVector) + public static double DotAndDot(this decimal[] rowVector, double[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -25765,48 +25909,51 @@ public static unsafe double DotAndDot(this decimal[] rowVector, double[,] matrix #endif double result = 0; - fixed (decimal* r = rowVector) - fixed (double* a = matrix) - fixed (double* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (double* a = matrix) + fixed (double* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -25862,7 +26009,7 @@ public static int DotAndDot(this decimal[] rowVector, double[][] matrix, int[] c #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this decimal[] rowVector, double[,] matrix, int[] columnVector) + public static int DotAndDot(this decimal[] rowVector, double[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -25872,48 +26019,51 @@ public static unsafe int DotAndDot(this decimal[] rowVector, double[,] matrix, i #endif int result = 0; - fixed (decimal* r = rowVector) - fixed (double* a = matrix) - fixed (int* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (double* a = matrix) + fixed (int* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -25969,7 +26119,7 @@ public static float DotAndDot(this decimal[] rowVector, double[][] matrix, float #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this decimal[] rowVector, double[,] matrix, float[] columnVector) + public static float DotAndDot(this decimal[] rowVector, double[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -25979,48 +26129,51 @@ public static unsafe float DotAndDot(this decimal[] rowVector, double[,] matrix, #endif float result = 0; - fixed (decimal* r = rowVector) - fixed (double* a = matrix) - fixed (float* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (double* a = matrix) + fixed (float* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -26076,7 +26229,7 @@ public static decimal DotAndDot(this decimal[] rowVector, int[][] matrix, decima #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal DotAndDot(this decimal[] rowVector, int[,] matrix, decimal[] columnVector) + public static decimal DotAndDot(this decimal[] rowVector, int[,] matrix, decimal[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -26086,48 +26239,51 @@ public static unsafe decimal DotAndDot(this decimal[] rowVector, int[,] matrix, #endif decimal result = 0; - fixed (decimal* r = rowVector) - fixed (int* a = matrix) - fixed (decimal* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - decimal sum1 = 0, sum2 = 0; - decimal* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (decimal)((decimal)(*pa1++) * (decimal)(*pc)); - sum2 += (decimal)((decimal)(*pa2++) * (decimal)(*pc)); - pc++; - } - - result += (decimal)((decimal)(*pr++) * sum1); - result += (decimal)((decimal)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (int* a = matrix) + fixed (decimal* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - decimal sum = 0; - decimal* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + decimal sum1 = 0, sum2 = 0; + decimal* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (decimal)((decimal)(*pa1++) * (decimal)(*pc)); + sum2 += (decimal)((decimal)(*pa2++) * (decimal)(*pc)); + pc++; + } + + result += (decimal)((decimal)(*pr++) * sum1); + result += (decimal)((decimal)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (decimal)((decimal)(*pa1++) * (decimal)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + decimal sum = 0; + decimal* pc = c; - result += (decimal)((decimal)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (decimal)((decimal)(*pa1++) * (decimal)(*pc++)); + result += (decimal)((decimal)(*pr++) * sum); + } + } + } + return result; } @@ -26183,7 +26339,7 @@ public static int DotAndDot(this decimal[] rowVector, int[][] matrix, int[] colu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this decimal[] rowVector, int[,] matrix, int[] columnVector) + public static int DotAndDot(this decimal[] rowVector, int[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -26193,48 +26349,51 @@ public static unsafe int DotAndDot(this decimal[] rowVector, int[,] matrix, int[ #endif int result = 0; - fixed (decimal* r = rowVector) - fixed (int* a = matrix) - fixed (int* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (int* a = matrix) + fixed (int* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -26290,7 +26449,7 @@ public static double DotAndDot(this decimal[] rowVector, int[][] matrix, double[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this decimal[] rowVector, int[,] matrix, double[] columnVector) + public static double DotAndDot(this decimal[] rowVector, int[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -26300,48 +26459,51 @@ public static unsafe double DotAndDot(this decimal[] rowVector, int[,] matrix, d #endif double result = 0; - fixed (decimal* r = rowVector) - fixed (int* a = matrix) - fixed (double* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (int* a = matrix) + fixed (double* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -26397,7 +26559,7 @@ public static float DotAndDot(this decimal[] rowVector, int[][] matrix, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this decimal[] rowVector, int[,] matrix, float[] columnVector) + public static float DotAndDot(this decimal[] rowVector, int[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -26407,48 +26569,51 @@ public static unsafe float DotAndDot(this decimal[] rowVector, int[,] matrix, fl #endif float result = 0; - fixed (decimal* r = rowVector) - fixed (int* a = matrix) - fixed (float* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (int* a = matrix) + fixed (float* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -26504,7 +26669,7 @@ public static double DotAndDot(this decimal[] rowVector, float[][] matrix, decim #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this decimal[] rowVector, float[,] matrix, decimal[] columnVector) + public static double DotAndDot(this decimal[] rowVector, float[,] matrix, decimal[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -26514,48 +26679,51 @@ public static unsafe double DotAndDot(this decimal[] rowVector, float[,] matrix, #endif double result = 0; - fixed (decimal* r = rowVector) - fixed (float* a = matrix) - fixed (decimal* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - decimal* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (float* a = matrix) + fixed (decimal* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - decimal* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + decimal* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + decimal* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -26611,7 +26779,7 @@ public static double DotAndDot(this decimal[] rowVector, float[][] matrix, float #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this decimal[] rowVector, float[,] matrix, float[] columnVector) + public static double DotAndDot(this decimal[] rowVector, float[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -26621,48 +26789,51 @@ public static unsafe double DotAndDot(this decimal[] rowVector, float[,] matrix, #endif double result = 0; - fixed (decimal* r = rowVector) - fixed (float* a = matrix) - fixed (float* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (float* a = matrix) + fixed (float* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -26718,7 +26889,7 @@ public static double DotAndDot(this decimal[] rowVector, float[][] matrix, doubl #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this decimal[] rowVector, float[,] matrix, double[] columnVector) + public static double DotAndDot(this decimal[] rowVector, float[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -26728,48 +26899,51 @@ public static unsafe double DotAndDot(this decimal[] rowVector, float[,] matrix, #endif double result = 0; - fixed (decimal* r = rowVector) - fixed (float* a = matrix) - fixed (double* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (float* a = matrix) + fixed (double* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -26825,7 +26999,7 @@ public static double DotAndDot(this decimal[] rowVector, float[][] matrix, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this decimal[] rowVector, float[,] matrix, int[] columnVector) + public static double DotAndDot(this decimal[] rowVector, float[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -26835,48 +27009,51 @@ public static unsafe double DotAndDot(this decimal[] rowVector, float[,] matrix, #endif double result = 0; - fixed (decimal* r = rowVector) - fixed (float* a = matrix) - fixed (int* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (decimal* r = rowVector) + fixed (float* a = matrix) + fixed (int* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + decimal* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -26932,7 +27109,7 @@ public static byte DotAndDot(this byte[] rowVector, byte[][] matrix, byte[] colu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte DotAndDot(this byte[] rowVector, byte[,] matrix, byte[] columnVector) + public static byte DotAndDot(this byte[] rowVector, byte[,] matrix, byte[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -26942,48 +27119,51 @@ public static unsafe byte DotAndDot(this byte[] rowVector, byte[,] matrix, byte[ #endif byte result = 0; - fixed (byte* r = rowVector) - fixed (byte* a = matrix) - fixed (byte* c = columnVector) - { - byte* pa1 = a; - byte* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - byte sum1 = 0, sum2 = 0; - byte* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (byte)((byte)(*pa1++) * (byte)(*pc)); - sum2 += (byte)((byte)(*pa2++) * (byte)(*pc)); - pc++; - } - - result += (byte)((byte)(*pr++) * sum1); - result += (byte)((byte)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (byte* a = matrix) + fixed (byte* c = columnVector) + { + byte* pa1 = a; + byte* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - byte sum = 0; - byte* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + byte sum1 = 0, sum2 = 0; + byte* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (byte)((byte)(*pa1++) * (byte)(*pc)); + sum2 += (byte)((byte)(*pa2++) * (byte)(*pc)); + pc++; + } + + result += (byte)((byte)(*pr++) * sum1); + result += (byte)((byte)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (byte)((byte)(*pa1++) * (byte)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + byte sum = 0; + byte* pc = c; - result += (byte)((byte)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (byte)((byte)(*pa1++) * (byte)(*pc++)); + result += (byte)((byte)(*pr++) * sum); + } + } + } + return result; } @@ -27039,7 +27219,7 @@ public static double DotAndDot(this byte[] rowVector, byte[][] matrix, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this byte[] rowVector, byte[,] matrix, double[] columnVector) + public static double DotAndDot(this byte[] rowVector, byte[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -27049,48 +27229,51 @@ public static unsafe double DotAndDot(this byte[] rowVector, byte[,] matrix, dou #endif double result = 0; - fixed (byte* r = rowVector) - fixed (byte* a = matrix) - fixed (double* c = columnVector) - { - byte* pa1 = a; - byte* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (byte* a = matrix) + fixed (double* c = columnVector) + { + byte* pa1 = a; + byte* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -27146,7 +27329,7 @@ public static int DotAndDot(this byte[] rowVector, byte[][] matrix, int[] column #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this byte[] rowVector, byte[,] matrix, int[] columnVector) + public static int DotAndDot(this byte[] rowVector, byte[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -27156,48 +27339,51 @@ public static unsafe int DotAndDot(this byte[] rowVector, byte[,] matrix, int[] #endif int result = 0; - fixed (byte* r = rowVector) - fixed (byte* a = matrix) - fixed (int* c = columnVector) - { - byte* pa1 = a; - byte* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (byte* a = matrix) + fixed (int* c = columnVector) + { + byte* pa1 = a; + byte* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -27253,7 +27439,7 @@ public static float DotAndDot(this byte[] rowVector, byte[][] matrix, float[] co #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this byte[] rowVector, byte[,] matrix, float[] columnVector) + public static float DotAndDot(this byte[] rowVector, byte[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -27263,48 +27449,51 @@ public static unsafe float DotAndDot(this byte[] rowVector, byte[,] matrix, floa #endif float result = 0; - fixed (byte* r = rowVector) - fixed (byte* a = matrix) - fixed (float* c = columnVector) - { - byte* pa1 = a; - byte* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (byte* a = matrix) + fixed (float* c = columnVector) + { + byte* pa1 = a; + byte* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -27360,7 +27549,7 @@ public static byte DotAndDot(this byte[] rowVector, double[][] matrix, byte[] co #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte DotAndDot(this byte[] rowVector, double[,] matrix, byte[] columnVector) + public static byte DotAndDot(this byte[] rowVector, double[,] matrix, byte[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -27370,48 +27559,51 @@ public static unsafe byte DotAndDot(this byte[] rowVector, double[,] matrix, byt #endif byte result = 0; - fixed (byte* r = rowVector) - fixed (double* a = matrix) - fixed (byte* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - byte sum1 = 0, sum2 = 0; - byte* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (byte)((byte)(*pa1++) * (byte)(*pc)); - sum2 += (byte)((byte)(*pa2++) * (byte)(*pc)); - pc++; - } - - result += (byte)((byte)(*pr++) * sum1); - result += (byte)((byte)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (double* a = matrix) + fixed (byte* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - byte sum = 0; - byte* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + byte sum1 = 0, sum2 = 0; + byte* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (byte)((byte)(*pa1++) * (byte)(*pc)); + sum2 += (byte)((byte)(*pa2++) * (byte)(*pc)); + pc++; + } + + result += (byte)((byte)(*pr++) * sum1); + result += (byte)((byte)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (byte)((byte)(*pa1++) * (byte)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + byte sum = 0; + byte* pc = c; - result += (byte)((byte)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (byte)((byte)(*pa1++) * (byte)(*pc++)); + result += (byte)((byte)(*pr++) * sum); + } + } + } + return result; } @@ -27467,7 +27659,7 @@ public static double DotAndDot(this byte[] rowVector, double[][] matrix, double[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this byte[] rowVector, double[,] matrix, double[] columnVector) + public static double DotAndDot(this byte[] rowVector, double[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -27477,48 +27669,51 @@ public static unsafe double DotAndDot(this byte[] rowVector, double[,] matrix, d #endif double result = 0; - fixed (byte* r = rowVector) - fixed (double* a = matrix) - fixed (double* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); + unsafe + { + fixed (byte* r = rowVector) + fixed (double* a = matrix) + fixed (double* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + byte* pr = r; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); - - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -27574,7 +27769,7 @@ public static int DotAndDot(this byte[] rowVector, double[][] matrix, int[] colu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this byte[] rowVector, double[,] matrix, int[] columnVector) + public static int DotAndDot(this byte[] rowVector, double[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -27584,48 +27779,51 @@ public static unsafe int DotAndDot(this byte[] rowVector, double[,] matrix, int[ #endif int result = 0; - fixed (byte* r = rowVector) - fixed (double* a = matrix) - fixed (int* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (double* a = matrix) + fixed (int* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -27681,7 +27879,7 @@ public static float DotAndDot(this byte[] rowVector, double[][] matrix, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this byte[] rowVector, double[,] matrix, float[] columnVector) + public static float DotAndDot(this byte[] rowVector, double[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -27691,48 +27889,51 @@ public static unsafe float DotAndDot(this byte[] rowVector, double[,] matrix, fl #endif float result = 0; - fixed (byte* r = rowVector) - fixed (double* a = matrix) - fixed (float* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (double* a = matrix) + fixed (float* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -27788,7 +27989,7 @@ public static byte DotAndDot(this byte[] rowVector, int[][] matrix, byte[] colum #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte DotAndDot(this byte[] rowVector, int[,] matrix, byte[] columnVector) + public static byte DotAndDot(this byte[] rowVector, int[,] matrix, byte[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -27798,48 +27999,51 @@ public static unsafe byte DotAndDot(this byte[] rowVector, int[,] matrix, byte[] #endif byte result = 0; - fixed (byte* r = rowVector) - fixed (int* a = matrix) - fixed (byte* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - byte sum1 = 0, sum2 = 0; - byte* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (byte)((byte)(*pa1++) * (byte)(*pc)); - sum2 += (byte)((byte)(*pa2++) * (byte)(*pc)); - pc++; - } - - result += (byte)((byte)(*pr++) * sum1); - result += (byte)((byte)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (int* a = matrix) + fixed (byte* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - byte sum = 0; - byte* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + byte sum1 = 0, sum2 = 0; + byte* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (byte)((byte)(*pa1++) * (byte)(*pc)); + sum2 += (byte)((byte)(*pa2++) * (byte)(*pc)); + pc++; + } + + result += (byte)((byte)(*pr++) * sum1); + result += (byte)((byte)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (byte)((byte)(*pa1++) * (byte)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + byte sum = 0; + byte* pc = c; - result += (byte)((byte)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (byte)((byte)(*pa1++) * (byte)(*pc++)); + result += (byte)((byte)(*pr++) * sum); + } + } + } + return result; } @@ -27895,7 +28099,7 @@ public static int DotAndDot(this byte[] rowVector, int[][] matrix, int[] columnV #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this byte[] rowVector, int[,] matrix, int[] columnVector) + public static int DotAndDot(this byte[] rowVector, int[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -27905,48 +28109,51 @@ public static unsafe int DotAndDot(this byte[] rowVector, int[,] matrix, int[] c #endif int result = 0; - fixed (byte* r = rowVector) - fixed (int* a = matrix) - fixed (int* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (int* a = matrix) + fixed (int* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -28002,7 +28209,7 @@ public static double DotAndDot(this byte[] rowVector, int[][] matrix, double[] c #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this byte[] rowVector, int[,] matrix, double[] columnVector) + public static double DotAndDot(this byte[] rowVector, int[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -28012,48 +28219,51 @@ public static unsafe double DotAndDot(this byte[] rowVector, int[,] matrix, doub #endif double result = 0; - fixed (byte* r = rowVector) - fixed (int* a = matrix) - fixed (double* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (int* a = matrix) + fixed (double* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -28109,7 +28319,7 @@ public static float DotAndDot(this byte[] rowVector, int[][] matrix, float[] col #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this byte[] rowVector, int[,] matrix, float[] columnVector) + public static float DotAndDot(this byte[] rowVector, int[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -28119,48 +28329,51 @@ public static unsafe float DotAndDot(this byte[] rowVector, int[,] matrix, float #endif float result = 0; - fixed (byte* r = rowVector) - fixed (int* a = matrix) - fixed (float* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (int* a = matrix) + fixed (float* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -28216,7 +28429,7 @@ public static double DotAndDot(this byte[] rowVector, float[][] matrix, byte[] c #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this byte[] rowVector, float[,] matrix, byte[] columnVector) + public static double DotAndDot(this byte[] rowVector, float[,] matrix, byte[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -28226,48 +28439,51 @@ public static unsafe double DotAndDot(this byte[] rowVector, float[,] matrix, by #endif double result = 0; - fixed (byte* r = rowVector) - fixed (float* a = matrix) - fixed (byte* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - byte* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (float* a = matrix) + fixed (byte* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - byte* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + byte* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + byte* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -28323,7 +28539,7 @@ public static double DotAndDot(this byte[] rowVector, float[][] matrix, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this byte[] rowVector, float[,] matrix, float[] columnVector) + public static double DotAndDot(this byte[] rowVector, float[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -28333,48 +28549,51 @@ public static unsafe double DotAndDot(this byte[] rowVector, float[,] matrix, fl #endif double result = 0; - fixed (byte* r = rowVector) - fixed (float* a = matrix) - fixed (float* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (float* a = matrix) + fixed (float* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -28430,7 +28649,7 @@ public static double DotAndDot(this byte[] rowVector, float[][] matrix, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this byte[] rowVector, float[,] matrix, double[] columnVector) + public static double DotAndDot(this byte[] rowVector, float[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -28440,48 +28659,51 @@ public static unsafe double DotAndDot(this byte[] rowVector, float[,] matrix, do #endif double result = 0; - fixed (byte* r = rowVector) - fixed (float* a = matrix) - fixed (double* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (float* a = matrix) + fixed (double* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -28537,7 +28759,7 @@ public static double DotAndDot(this byte[] rowVector, float[][] matrix, int[] co #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this byte[] rowVector, float[,] matrix, int[] columnVector) + public static double DotAndDot(this byte[] rowVector, float[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -28547,48 +28769,51 @@ public static unsafe double DotAndDot(this byte[] rowVector, float[,] matrix, in #endif double result = 0; - fixed (byte* r = rowVector) - fixed (float* a = matrix) - fixed (int* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (byte* r = rowVector) + fixed (float* a = matrix) + fixed (int* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + byte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -28644,7 +28869,7 @@ public static short DotAndDot(this short[] rowVector, short[][] matrix, short[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short DotAndDot(this short[] rowVector, short[,] matrix, short[] columnVector) + public static short DotAndDot(this short[] rowVector, short[,] matrix, short[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -28654,48 +28879,51 @@ public static unsafe short DotAndDot(this short[] rowVector, short[,] matrix, sh #endif short result = 0; - fixed (short* r = rowVector) - fixed (short* a = matrix) - fixed (short* c = columnVector) - { - short* pa1 = a; - short* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - short sum1 = 0, sum2 = 0; - short* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (short)((short)(*pa1++) * (short)(*pc)); - sum2 += (short)((short)(*pa2++) * (short)(*pc)); - pc++; - } - - result += (short)((short)(*pr++) * sum1); - result += (short)((short)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (short* r = rowVector) + fixed (short* a = matrix) + fixed (short* c = columnVector) + { + short* pa1 = a; + short* pa2 = a + cols; + short* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - short sum = 0; - short* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + short sum1 = 0, sum2 = 0; + short* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (short)((short)(*pa1++) * (short)(*pc)); + sum2 += (short)((short)(*pa2++) * (short)(*pc)); + pc++; + } + + result += (short)((short)(*pr++) * sum1); + result += (short)((short)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (short)((short)(*pa1++) * (short)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + short sum = 0; + short* pc = c; - result += (short)((short)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (short)((short)(*pa1++) * (short)(*pc++)); + result += (short)((short)(*pr++) * sum); + } + } + } + return result; } @@ -28751,7 +28979,7 @@ public static double DotAndDot(this short[] rowVector, short[][] matrix, double[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this short[] rowVector, short[,] matrix, double[] columnVector) + public static double DotAndDot(this short[] rowVector, short[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -28761,48 +28989,51 @@ public static unsafe double DotAndDot(this short[] rowVector, short[,] matrix, d #endif double result = 0; - fixed (short* r = rowVector) - fixed (short* a = matrix) - fixed (double* c = columnVector) - { - short* pa1 = a; - short* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (short* r = rowVector) + fixed (short* a = matrix) + fixed (double* c = columnVector) + { + short* pa1 = a; + short* pa2 = a + cols; + short* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -28858,7 +29089,7 @@ public static int DotAndDot(this short[] rowVector, short[][] matrix, int[] colu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this short[] rowVector, short[,] matrix, int[] columnVector) + public static int DotAndDot(this short[] rowVector, short[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -28868,48 +29099,51 @@ public static unsafe int DotAndDot(this short[] rowVector, short[,] matrix, int[ #endif int result = 0; - fixed (short* r = rowVector) - fixed (short* a = matrix) - fixed (int* c = columnVector) - { - short* pa1 = a; - short* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); + unsafe + { + fixed (short* r = rowVector) + fixed (short* a = matrix) + fixed (int* c = columnVector) + { + short* pa1 = a; + short* pa2 = a + cols; + short* pr = r; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); - - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -28965,7 +29199,7 @@ public static float DotAndDot(this short[] rowVector, short[][] matrix, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this short[] rowVector, short[,] matrix, float[] columnVector) + public static float DotAndDot(this short[] rowVector, short[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -28975,48 +29209,51 @@ public static unsafe float DotAndDot(this short[] rowVector, short[,] matrix, fl #endif float result = 0; - fixed (short* r = rowVector) - fixed (short* a = matrix) - fixed (float* c = columnVector) - { - short* pa1 = a; - short* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (short* r = rowVector) + fixed (short* a = matrix) + fixed (float* c = columnVector) + { + short* pa1 = a; + short* pa2 = a + cols; + short* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -29072,7 +29309,7 @@ public static short DotAndDot(this short[] rowVector, double[][] matrix, short[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short DotAndDot(this short[] rowVector, double[,] matrix, short[] columnVector) + public static short DotAndDot(this short[] rowVector, double[,] matrix, short[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -29082,48 +29319,51 @@ public static unsafe short DotAndDot(this short[] rowVector, double[,] matrix, s #endif short result = 0; - fixed (short* r = rowVector) - fixed (double* a = matrix) - fixed (short* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - short sum1 = 0, sum2 = 0; - short* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (short)((short)(*pa1++) * (short)(*pc)); - sum2 += (short)((short)(*pa2++) * (short)(*pc)); - pc++; - } - - result += (short)((short)(*pr++) * sum1); - result += (short)((short)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (short* r = rowVector) + fixed (double* a = matrix) + fixed (short* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + short* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - short sum = 0; - short* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + short sum1 = 0, sum2 = 0; + short* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (short)((short)(*pa1++) * (short)(*pc)); + sum2 += (short)((short)(*pa2++) * (short)(*pc)); + pc++; + } + + result += (short)((short)(*pr++) * sum1); + result += (short)((short)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (short)((short)(*pa1++) * (short)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + short sum = 0; + short* pc = c; - result += (short)((short)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (short)((short)(*pa1++) * (short)(*pc++)); + result += (short)((short)(*pr++) * sum); + } + } + } + return result; } @@ -29179,7 +29419,7 @@ public static double DotAndDot(this short[] rowVector, double[][] matrix, double #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this short[] rowVector, double[,] matrix, double[] columnVector) + public static double DotAndDot(this short[] rowVector, double[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -29189,48 +29429,51 @@ public static unsafe double DotAndDot(this short[] rowVector, double[,] matrix, #endif double result = 0; - fixed (short* r = rowVector) - fixed (double* a = matrix) - fixed (double* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (short* r = rowVector) + fixed (double* a = matrix) + fixed (double* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + short* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -29286,7 +29529,7 @@ public static int DotAndDot(this short[] rowVector, double[][] matrix, int[] col #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this short[] rowVector, double[,] matrix, int[] columnVector) + public static int DotAndDot(this short[] rowVector, double[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -29296,48 +29539,51 @@ public static unsafe int DotAndDot(this short[] rowVector, double[,] matrix, int #endif int result = 0; - fixed (short* r = rowVector) - fixed (double* a = matrix) - fixed (int* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (short* r = rowVector) + fixed (double* a = matrix) + fixed (int* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + short* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -29393,7 +29639,7 @@ public static float DotAndDot(this short[] rowVector, double[][] matrix, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this short[] rowVector, double[,] matrix, float[] columnVector) + public static float DotAndDot(this short[] rowVector, double[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -29403,48 +29649,51 @@ public static unsafe float DotAndDot(this short[] rowVector, double[,] matrix, f #endif float result = 0; - fixed (short* r = rowVector) - fixed (double* a = matrix) - fixed (float* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (short* r = rowVector) + fixed (double* a = matrix) + fixed (float* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + short* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -29500,7 +29749,7 @@ public static short DotAndDot(this short[] rowVector, int[][] matrix, short[] co #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short DotAndDot(this short[] rowVector, int[,] matrix, short[] columnVector) + public static short DotAndDot(this short[] rowVector, int[,] matrix, short[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -29510,48 +29759,51 @@ public static unsafe short DotAndDot(this short[] rowVector, int[,] matrix, shor #endif short result = 0; - fixed (short* r = rowVector) - fixed (int* a = matrix) - fixed (short* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - short sum1 = 0, sum2 = 0; - short* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (short)((short)(*pa1++) * (short)(*pc)); - sum2 += (short)((short)(*pa2++) * (short)(*pc)); - pc++; - } - - result += (short)((short)(*pr++) * sum1); - result += (short)((short)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (short* r = rowVector) + fixed (int* a = matrix) + fixed (short* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + short* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - short sum = 0; - short* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + short sum1 = 0, sum2 = 0; + short* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (short)((short)(*pa1++) * (short)(*pc)); + sum2 += (short)((short)(*pa2++) * (short)(*pc)); + pc++; + } + + result += (short)((short)(*pr++) * sum1); + result += (short)((short)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (short)((short)(*pa1++) * (short)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + short sum = 0; + short* pc = c; - result += (short)((short)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (short)((short)(*pa1++) * (short)(*pc++)); + result += (short)((short)(*pr++) * sum); + } + } + } + return result; } @@ -29607,7 +29859,7 @@ public static int DotAndDot(this short[] rowVector, int[][] matrix, int[] column #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this short[] rowVector, int[,] matrix, int[] columnVector) + public static int DotAndDot(this short[] rowVector, int[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -29617,48 +29869,51 @@ public static unsafe int DotAndDot(this short[] rowVector, int[,] matrix, int[] #endif int result = 0; - fixed (short* r = rowVector) - fixed (int* a = matrix) - fixed (int* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (short* r = rowVector) + fixed (int* a = matrix) + fixed (int* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + short* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -29714,7 +29969,7 @@ public static double DotAndDot(this short[] rowVector, int[][] matrix, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this short[] rowVector, int[,] matrix, double[] columnVector) + public static double DotAndDot(this short[] rowVector, int[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -29724,48 +29979,51 @@ public static unsafe double DotAndDot(this short[] rowVector, int[,] matrix, dou #endif double result = 0; - fixed (short* r = rowVector) - fixed (int* a = matrix) - fixed (double* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (short* r = rowVector) + fixed (int* a = matrix) + fixed (double* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + short* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -29821,7 +30079,7 @@ public static float DotAndDot(this short[] rowVector, int[][] matrix, float[] co #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this short[] rowVector, int[,] matrix, float[] columnVector) + public static float DotAndDot(this short[] rowVector, int[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -29831,48 +30089,51 @@ public static unsafe float DotAndDot(this short[] rowVector, int[,] matrix, floa #endif float result = 0; - fixed (short* r = rowVector) - fixed (int* a = matrix) - fixed (float* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (short* r = rowVector) + fixed (int* a = matrix) + fixed (float* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + short* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -29928,7 +30189,7 @@ public static double DotAndDot(this short[] rowVector, float[][] matrix, short[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this short[] rowVector, float[,] matrix, short[] columnVector) + public static double DotAndDot(this short[] rowVector, float[,] matrix, short[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -29938,48 +30199,51 @@ public static unsafe double DotAndDot(this short[] rowVector, float[,] matrix, s #endif double result = 0; - fixed (short* r = rowVector) - fixed (float* a = matrix) - fixed (short* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - short* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (short* r = rowVector) + fixed (float* a = matrix) + fixed (short* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + short* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - short* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + short* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + short* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -30035,7 +30299,7 @@ public static double DotAndDot(this short[] rowVector, float[][] matrix, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this short[] rowVector, float[,] matrix, float[] columnVector) + public static double DotAndDot(this short[] rowVector, float[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -30045,48 +30309,51 @@ public static unsafe double DotAndDot(this short[] rowVector, float[,] matrix, f #endif double result = 0; - fixed (short* r = rowVector) - fixed (float* a = matrix) - fixed (float* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (short* r = rowVector) + fixed (float* a = matrix) + fixed (float* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + short* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -30142,7 +30409,7 @@ public static double DotAndDot(this short[] rowVector, float[][] matrix, double[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this short[] rowVector, float[,] matrix, double[] columnVector) + public static double DotAndDot(this short[] rowVector, float[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -30152,155 +30419,51 @@ public static unsafe double DotAndDot(this short[] rowVector, float[,] matrix, d #endif double result = 0; - fixed (short* r = rowVector) - fixed (float* a = matrix) - fixed (double* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } - - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); - - result += (double)((double)(*pr++) * sum); - } - } - - return result; - } + unsafe + { + fixed (short* r = rowVector) + fixed (float* a = matrix) + fixed (double* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + short* pr = r; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); - /// - /// Computes the product a*B*c of a row vector a, - /// a square matrix B and a column vector c. - /// - /// - /// The left vector a. - /// The square matrix B. - /// The column vector c. - /// - /// The product a*B*c of the given vector a, - /// matrix B and vector c. - /// -#if NET45 || NET46 || NET462 || NETSTANDARD - [MethodImpl(MethodImplOptions.AggressiveInlining)] -#endif - public static double DotAndDot(this short[] rowVector, float[][] matrix, int[] columnVector) - { -#if DEBUG - if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) - throw new DimensionMismatchException(); -#endif - double sum = 0; + result += (double)((double)(*pr++) * sum); + } + } + } - for (int i = 0; i < rowVector.Length; i++) - { - double s = 0; - for (int j = 0; j < columnVector.Length; j++) - s += (double)((double)matrix[i][j] * (double)columnVector[j]); - sum += (double)((double)rowVector[i] * (double)s); - } - - return sum; - } - - /// - /// Computes the product a*B*c of a row vector a, - /// a square matrix B and a column vector c. - /// - /// - /// The left vector a. - /// The square matrix B. - /// The column vector c. - /// - /// The product a*B*c of the given vector a, - /// matrix B and vector c. - /// -#if NET45 || NET46 || NET462 || NETSTANDARD - [MethodImpl(MethodImplOptions.AggressiveInlining)] -#endif - public static unsafe double DotAndDot(this short[] rowVector, float[,] matrix, int[] columnVector) - { - int cols = matrix.Columns(); - int rows = matrix.Rows(); -#if DEBUG - if (rowVector.Length != rows || cols != columnVector.Length) - throw new DimensionMismatchException(); -#endif - double result = 0; - - fixed (short* r = rowVector) - fixed (float* a = matrix) - fixed (int* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } - - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); - - result += (double)((double)(*pr++) * sum); - } - } - return result; } @@ -30322,20 +30485,20 @@ public static unsafe double DotAndDot(this short[] rowVector, float[,] matrix, i #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static sbyte DotAndDot(this sbyte[] rowVector, sbyte[][] matrix, sbyte[] columnVector) + public static double DotAndDot(this short[] rowVector, float[][] matrix, int[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - sbyte sum = 0; + double sum = 0; for (int i = 0; i < rowVector.Length; i++) { - sbyte s = 0; + double s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (sbyte)((sbyte)matrix[i][j] * (sbyte)columnVector[j]); - sum += (sbyte)((sbyte)rowVector[i] * (sbyte)s); + s += (double)((double)matrix[i][j] * (double)columnVector[j]); + sum += (double)((double)rowVector[i] * (double)s); } return sum; @@ -30356,7 +30519,7 @@ public static sbyte DotAndDot(this sbyte[] rowVector, sbyte[][] matrix, sbyte[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, sbyte[] columnVector) + public static double DotAndDot(this short[] rowVector, float[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -30364,50 +30527,53 @@ public static unsafe sbyte DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, sb if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - sbyte result = 0; - - fixed (sbyte* r = rowVector) - fixed (sbyte* a = matrix) - fixed (sbyte* c = columnVector) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - sbyte sum1 = 0, sum2 = 0; - sbyte* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (sbyte)((sbyte)(*pa1++) * (sbyte)(*pc)); - sum2 += (sbyte)((sbyte)(*pa2++) * (sbyte)(*pc)); - pc++; - } - - result += (sbyte)((sbyte)(*pr++) * sum1); - result += (sbyte)((sbyte)(*pr++) * sum2); + double result = 0; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (short* r = rowVector) + fixed (float* a = matrix) + fixed (int* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + short* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - sbyte sum = 0; - sbyte* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (sbyte)((sbyte)(*pa1++) * (sbyte)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* pc = c; - result += (sbyte)((sbyte)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -30429,20 +30595,20 @@ public static unsafe sbyte DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, sb #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static double DotAndDot(this sbyte[] rowVector, sbyte[][] matrix, double[] columnVector) + public static sbyte DotAndDot(this sbyte[] rowVector, sbyte[][] matrix, sbyte[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - double sum = 0; + sbyte sum = 0; for (int i = 0; i < rowVector.Length; i++) { - double s = 0; + sbyte s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (double)((double)matrix[i][j] * (double)columnVector[j]); - sum += (double)((double)rowVector[i] * (double)s); + s += (sbyte)((sbyte)matrix[i][j] * (sbyte)columnVector[j]); + sum += (sbyte)((sbyte)rowVector[i] * (sbyte)s); } return sum; @@ -30463,7 +30629,7 @@ public static double DotAndDot(this sbyte[] rowVector, sbyte[][] matrix, double[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, double[] columnVector) + public static sbyte DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, sbyte[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -30471,50 +30637,53 @@ public static unsafe double DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, d if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - double result = 0; - - fixed (sbyte* r = rowVector) - fixed (sbyte* a = matrix) - fixed (double* c = columnVector) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); + sbyte result = 0; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (sbyte* r = rowVector) + fixed (sbyte* a = matrix) + fixed (sbyte* c = columnVector) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + sbyte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + sbyte sum1 = 0, sum2 = 0; + sbyte* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (sbyte)((sbyte)(*pa1++) * (sbyte)(*pc)); + sum2 += (sbyte)((sbyte)(*pa2++) * (sbyte)(*pc)); + pc++; + } + + result += (sbyte)((sbyte)(*pr++) * sum1); + result += (sbyte)((sbyte)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + sbyte sum = 0; + sbyte* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (sbyte)((sbyte)(*pa1++) * (sbyte)(*pc++)); + result += (sbyte)((sbyte)(*pr++) * sum); + } + } + } + return result; } @@ -30536,20 +30705,20 @@ public static unsafe double DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, d #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static int DotAndDot(this sbyte[] rowVector, sbyte[][] matrix, int[] columnVector) + public static double DotAndDot(this sbyte[] rowVector, sbyte[][] matrix, double[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - int sum = 0; + double sum = 0; for (int i = 0; i < rowVector.Length; i++) { - int s = 0; + double s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (int)((int)matrix[i][j] * (int)columnVector[j]); - sum += (int)((int)rowVector[i] * (int)s); + s += (double)((double)matrix[i][j] * (double)columnVector[j]); + sum += (double)((double)rowVector[i] * (double)s); } return sum; @@ -30570,7 +30739,7 @@ public static int DotAndDot(this sbyte[] rowVector, sbyte[][] matrix, int[] colu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, int[] columnVector) + public static double DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -30578,50 +30747,53 @@ public static unsafe int DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, int[ if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - int result = 0; - - fixed (sbyte* r = rowVector) - fixed (sbyte* a = matrix) - fixed (int* c = columnVector) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); + double result = 0; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (sbyte* r = rowVector) + fixed (sbyte* a = matrix) + fixed (double* c = columnVector) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + sbyte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -30643,20 +30815,20 @@ public static unsafe int DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, int[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static float DotAndDot(this sbyte[] rowVector, sbyte[][] matrix, float[] columnVector) + public static int DotAndDot(this sbyte[] rowVector, sbyte[][] matrix, int[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - float sum = 0; + int sum = 0; for (int i = 0; i < rowVector.Length; i++) { - float s = 0; + int s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (float)((float)matrix[i][j] * (float)columnVector[j]); - sum += (float)((float)rowVector[i] * (float)s); + s += (int)((int)matrix[i][j] * (int)columnVector[j]); + sum += (int)((int)rowVector[i] * (int)s); } return sum; @@ -30677,7 +30849,7 @@ public static float DotAndDot(this sbyte[] rowVector, sbyte[][] matrix, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, float[] columnVector) + public static int DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -30685,50 +30857,53 @@ public static unsafe float DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, fl if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - float result = 0; - - fixed (sbyte* r = rowVector) - fixed (sbyte* a = matrix) - fixed (float* c = columnVector) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); + int result = 0; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (sbyte* r = rowVector) + fixed (sbyte* a = matrix) + fixed (int* c = columnVector) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + sbyte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -30750,20 +30925,20 @@ public static unsafe float DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, fl #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static sbyte DotAndDot(this sbyte[] rowVector, double[][] matrix, sbyte[] columnVector) + public static float DotAndDot(this sbyte[] rowVector, sbyte[][] matrix, float[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - sbyte sum = 0; + float sum = 0; for (int i = 0; i < rowVector.Length; i++) { - sbyte s = 0; + float s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (sbyte)((sbyte)matrix[i][j] * (sbyte)columnVector[j]); - sum += (sbyte)((sbyte)rowVector[i] * (sbyte)s); + s += (float)((float)matrix[i][j] * (float)columnVector[j]); + sum += (float)((float)rowVector[i] * (float)s); } return sum; @@ -30784,7 +30959,7 @@ public static sbyte DotAndDot(this sbyte[] rowVector, double[][] matrix, sbyte[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte DotAndDot(this sbyte[] rowVector, double[,] matrix, sbyte[] columnVector) + public static float DotAndDot(this sbyte[] rowVector, sbyte[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -30792,50 +30967,53 @@ public static unsafe sbyte DotAndDot(this sbyte[] rowVector, double[,] matrix, s if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - sbyte result = 0; - - fixed (sbyte* r = rowVector) - fixed (double* a = matrix) - fixed (sbyte* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - sbyte sum1 = 0, sum2 = 0; - sbyte* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (sbyte)((sbyte)(*pa1++) * (sbyte)(*pc)); - sum2 += (sbyte)((sbyte)(*pa2++) * (sbyte)(*pc)); - pc++; - } - - result += (sbyte)((sbyte)(*pr++) * sum1); - result += (sbyte)((sbyte)(*pr++) * sum2); + float result = 0; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (sbyte* r = rowVector) + fixed (sbyte* a = matrix) + fixed (float* c = columnVector) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + sbyte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - sbyte sum = 0; - sbyte* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (sbyte)((sbyte)(*pa1++) * (sbyte)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (sbyte)((sbyte)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -30857,20 +31035,20 @@ public static unsafe sbyte DotAndDot(this sbyte[] rowVector, double[,] matrix, s #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static double DotAndDot(this sbyte[] rowVector, double[][] matrix, double[] columnVector) + public static sbyte DotAndDot(this sbyte[] rowVector, double[][] matrix, sbyte[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - double sum = 0; + sbyte sum = 0; for (int i = 0; i < rowVector.Length; i++) { - double s = 0; + sbyte s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (double)((double)matrix[i][j] * (double)columnVector[j]); - sum += (double)((double)rowVector[i] * (double)s); + s += (sbyte)((sbyte)matrix[i][j] * (sbyte)columnVector[j]); + sum += (sbyte)((sbyte)rowVector[i] * (sbyte)s); } return sum; @@ -30891,7 +31069,7 @@ public static double DotAndDot(this sbyte[] rowVector, double[][] matrix, double #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this sbyte[] rowVector, double[,] matrix, double[] columnVector) + public static sbyte DotAndDot(this sbyte[] rowVector, double[,] matrix, sbyte[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -30899,50 +31077,53 @@ public static unsafe double DotAndDot(this sbyte[] rowVector, double[,] matrix, if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - double result = 0; - - fixed (sbyte* r = rowVector) - fixed (double* a = matrix) - fixed (double* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); + sbyte result = 0; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (sbyte* r = rowVector) + fixed (double* a = matrix) + fixed (sbyte* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + sbyte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + sbyte sum1 = 0, sum2 = 0; + sbyte* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (sbyte)((sbyte)(*pa1++) * (sbyte)(*pc)); + sum2 += (sbyte)((sbyte)(*pa2++) * (sbyte)(*pc)); + pc++; + } + + result += (sbyte)((sbyte)(*pr++) * sum1); + result += (sbyte)((sbyte)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + sbyte sum = 0; + sbyte* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (sbyte)((sbyte)(*pa1++) * (sbyte)(*pc++)); + result += (sbyte)((sbyte)(*pr++) * sum); + } + } + } + return result; } @@ -30964,20 +31145,20 @@ public static unsafe double DotAndDot(this sbyte[] rowVector, double[,] matrix, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static int DotAndDot(this sbyte[] rowVector, double[][] matrix, int[] columnVector) + public static double DotAndDot(this sbyte[] rowVector, double[][] matrix, double[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - int sum = 0; + double sum = 0; for (int i = 0; i < rowVector.Length; i++) { - int s = 0; + double s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (int)((int)matrix[i][j] * (int)columnVector[j]); - sum += (int)((int)rowVector[i] * (int)s); + s += (double)((double)matrix[i][j] * (double)columnVector[j]); + sum += (double)((double)rowVector[i] * (double)s); } return sum; @@ -30998,7 +31179,7 @@ public static int DotAndDot(this sbyte[] rowVector, double[][] matrix, int[] col #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this sbyte[] rowVector, double[,] matrix, int[] columnVector) + public static double DotAndDot(this sbyte[] rowVector, double[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -31006,50 +31187,53 @@ public static unsafe int DotAndDot(this sbyte[] rowVector, double[,] matrix, int if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - int result = 0; - - fixed (sbyte* r = rowVector) - fixed (double* a = matrix) - fixed (int* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); + double result = 0; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (sbyte* r = rowVector) + fixed (double* a = matrix) + fixed (double* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + sbyte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -31071,20 +31255,20 @@ public static unsafe int DotAndDot(this sbyte[] rowVector, double[,] matrix, int #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static float DotAndDot(this sbyte[] rowVector, double[][] matrix, float[] columnVector) + public static int DotAndDot(this sbyte[] rowVector, double[][] matrix, int[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - float sum = 0; + int sum = 0; for (int i = 0; i < rowVector.Length; i++) { - float s = 0; + int s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (float)((float)matrix[i][j] * (float)columnVector[j]); - sum += (float)((float)rowVector[i] * (float)s); + s += (int)((int)matrix[i][j] * (int)columnVector[j]); + sum += (int)((int)rowVector[i] * (int)s); } return sum; @@ -31105,7 +31289,7 @@ public static float DotAndDot(this sbyte[] rowVector, double[][] matrix, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this sbyte[] rowVector, double[,] matrix, float[] columnVector) + public static int DotAndDot(this sbyte[] rowVector, double[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -31113,50 +31297,53 @@ public static unsafe float DotAndDot(this sbyte[] rowVector, double[,] matrix, f if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - float result = 0; - - fixed (sbyte* r = rowVector) - fixed (double* a = matrix) - fixed (float* c = columnVector) - { - double* pa1 = a; - double* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); + int result = 0; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (sbyte* r = rowVector) + fixed (double* a = matrix) + fixed (int* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + sbyte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -31178,20 +31365,20 @@ public static unsafe float DotAndDot(this sbyte[] rowVector, double[,] matrix, f #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static sbyte DotAndDot(this sbyte[] rowVector, int[][] matrix, sbyte[] columnVector) + public static float DotAndDot(this sbyte[] rowVector, double[][] matrix, float[] columnVector) { #if DEBUG if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) throw new DimensionMismatchException(); #endif - sbyte sum = 0; + float sum = 0; for (int i = 0; i < rowVector.Length; i++) { - sbyte s = 0; + float s = 0; for (int j = 0; j < columnVector.Length; j++) - s += (sbyte)((sbyte)matrix[i][j] * (sbyte)columnVector[j]); - sum += (sbyte)((sbyte)rowVector[i] * (sbyte)s); + s += (float)((float)matrix[i][j] * (float)columnVector[j]); + sum += (float)((float)rowVector[i] * (float)s); } return sum; @@ -31212,7 +31399,7 @@ public static sbyte DotAndDot(this sbyte[] rowVector, int[][] matrix, sbyte[] co #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte DotAndDot(this sbyte[] rowVector, int[,] matrix, sbyte[] columnVector) + public static float DotAndDot(this sbyte[] rowVector, double[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -31220,50 +31407,163 @@ public static unsafe sbyte DotAndDot(this sbyte[] rowVector, int[,] matrix, sbyt if (rowVector.Length != rows || cols != columnVector.Length) throw new DimensionMismatchException(); #endif - sbyte result = 0; + float result = 0; - fixed (sbyte* r = rowVector) - fixed (int* a = matrix) - fixed (sbyte* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - sbyte* pr = r; + unsafe + { + fixed (sbyte* r = rowVector) + fixed (double* a = matrix) + fixed (float* c = columnVector) + { + double* pa1 = a; + double* pa2 = a + cols; + sbyte* pr = r; - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - sbyte sum1 = 0, sum2 = 0; - sbyte* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - { - sum1 += (sbyte)((sbyte)(*pa1++) * (sbyte)(*pc)); - sum2 += (sbyte)((sbyte)(*pa2++) * (sbyte)(*pc)); - pc++; - } + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (sbyte)((sbyte)(*pr++) * sum1); - result += (sbyte)((sbyte)(*pr++) * sum2); + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + result += (float)((float)(*pr++) * sum); + } + } + } + + return result; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - sbyte sum = 0; - sbyte* pc = c; - for (int j = 0; j < cols; j++) - sum += (sbyte)((sbyte)(*pa1++) * (sbyte)(*pc++)); - result += (sbyte)((sbyte)(*pr++) * sum); - } + + /// + /// Computes the product a*B*c of a row vector a, + /// a square matrix B and a column vector c. + /// + /// + /// The left vector a. + /// The square matrix B. + /// The column vector c. + /// + /// The product a*B*c of the given vector a, + /// matrix B and vector c. + /// +#if NET45 || NET46 || NET462 || NETSTANDARD + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif + public static sbyte DotAndDot(this sbyte[] rowVector, int[][] matrix, sbyte[] columnVector) + { +#if DEBUG + if (rowVector.Length != matrix.Rows() || matrix.Columns() != columnVector.Length) + throw new DimensionMismatchException(); +#endif + sbyte sum = 0; + + for (int i = 0; i < rowVector.Length; i++) + { + sbyte s = 0; + for (int j = 0; j < columnVector.Length; j++) + s += (sbyte)((sbyte)matrix[i][j] * (sbyte)columnVector[j]); + sum += (sbyte)((sbyte)rowVector[i] * (sbyte)s); } + return sum; + } + + /// + /// Computes the product a*B*c of a row vector a, + /// a square matrix B and a column vector c. + /// + /// + /// The left vector a. + /// The square matrix B. + /// The column vector c. + /// + /// The product a*B*c of the given vector a, + /// matrix B and vector c. + /// +#if NET45 || NET46 || NET462 || NETSTANDARD + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif + public static sbyte DotAndDot(this sbyte[] rowVector, int[,] matrix, sbyte[] columnVector) + { + int cols = matrix.Columns(); + int rows = matrix.Rows(); +#if DEBUG + if (rowVector.Length != rows || cols != columnVector.Length) + throw new DimensionMismatchException(); +#endif + sbyte result = 0; + + unsafe + { + fixed (sbyte* r = rowVector) + fixed (int* a = matrix) + fixed (sbyte* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + sbyte* pr = r; + + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + sbyte sum1 = 0, sum2 = 0; + sbyte* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (sbyte)((sbyte)(*pa1++) * (sbyte)(*pc)); + sum2 += (sbyte)((sbyte)(*pa2++) * (sbyte)(*pc)); + pc++; + } + + result += (sbyte)((sbyte)(*pr++) * sum1); + result += (sbyte)((sbyte)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } + + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + sbyte sum = 0; + sbyte* pc = c; + + for (int j = 0; j < cols; j++) + sum += (sbyte)((sbyte)(*pa1++) * (sbyte)(*pc++)); + + result += (sbyte)((sbyte)(*pr++) * sum); + } + } + } + return result; } @@ -31319,7 +31619,7 @@ public static int DotAndDot(this sbyte[] rowVector, int[][] matrix, int[] column #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int DotAndDot(this sbyte[] rowVector, int[,] matrix, int[] columnVector) + public static int DotAndDot(this sbyte[] rowVector, int[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -31329,48 +31629,51 @@ public static unsafe int DotAndDot(this sbyte[] rowVector, int[,] matrix, int[] #endif int result = 0; - fixed (sbyte* r = rowVector) - fixed (int* a = matrix) - fixed (int* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*pc)); - sum2 += (int)((int)(*pa2++) * (int)(*pc)); - pc++; - } - - result += (int)((int)(*pr++) * sum1); - result += (int)((int)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (sbyte* r = rowVector) + fixed (int* a = matrix) + fixed (int* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + sbyte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*pc)); + sum2 += (int)((int)(*pa2++) * (int)(*pc)); + pc++; + } + + result += (int)((int)(*pr++) * sum1); + result += (int)((int)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* pc = c; - result += (int)((int)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*pc++)); + result += (int)((int)(*pr++) * sum); + } + } + } + return result; } @@ -31426,7 +31729,7 @@ public static double DotAndDot(this sbyte[] rowVector, int[][] matrix, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this sbyte[] rowVector, int[,] matrix, double[] columnVector) + public static double DotAndDot(this sbyte[] rowVector, int[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -31436,48 +31739,51 @@ public static unsafe double DotAndDot(this sbyte[] rowVector, int[,] matrix, dou #endif double result = 0; - fixed (sbyte* r = rowVector) - fixed (int* a = matrix) - fixed (double* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (sbyte* r = rowVector) + fixed (int* a = matrix) + fixed (double* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + sbyte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -31533,7 +31839,7 @@ public static float DotAndDot(this sbyte[] rowVector, int[][] matrix, float[] co #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float DotAndDot(this sbyte[] rowVector, int[,] matrix, float[] columnVector) + public static float DotAndDot(this sbyte[] rowVector, int[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -31543,48 +31849,51 @@ public static unsafe float DotAndDot(this sbyte[] rowVector, int[,] matrix, floa #endif float result = 0; - fixed (sbyte* r = rowVector) - fixed (int* a = matrix) - fixed (float* c = columnVector) - { - int* pa1 = a; - int* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - float sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (float)((float)(*pa1++) * (float)(*pc)); - sum2 += (float)((float)(*pa2++) * (float)(*pc)); - pc++; - } - - result += (float)((float)(*pr++) * sum1); - result += (float)((float)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (sbyte* r = rowVector) + fixed (int* a = matrix) + fixed (float* c = columnVector) + { + int* pa1 = a; + int* pa2 = a + cols; + sbyte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - float sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + float sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (float)((float)(*pa1++) * (float)(*pc)); + sum2 += (float)((float)(*pa2++) * (float)(*pc)); + pc++; + } + + result += (float)((float)(*pr++) * sum1); + result += (float)((float)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (float)((float)(*pa1++) * (float)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + float sum = 0; + float* pc = c; - result += (float)((float)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (float)((float)(*pa1++) * (float)(*pc++)); + result += (float)((float)(*pr++) * sum); + } + } + } + return result; } @@ -31640,7 +31949,7 @@ public static double DotAndDot(this sbyte[] rowVector, float[][] matrix, sbyte[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this sbyte[] rowVector, float[,] matrix, sbyte[] columnVector) + public static double DotAndDot(this sbyte[] rowVector, float[,] matrix, sbyte[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -31650,48 +31959,51 @@ public static unsafe double DotAndDot(this sbyte[] rowVector, float[,] matrix, s #endif double result = 0; - fixed (sbyte* r = rowVector) - fixed (float* a = matrix) - fixed (sbyte* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - sbyte* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (sbyte* r = rowVector) + fixed (float* a = matrix) + fixed (sbyte* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + sbyte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - sbyte* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + sbyte* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + sbyte* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -31747,7 +32059,7 @@ public static double DotAndDot(this sbyte[] rowVector, float[][] matrix, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this sbyte[] rowVector, float[,] matrix, float[] columnVector) + public static double DotAndDot(this sbyte[] rowVector, float[,] matrix, float[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -31757,48 +32069,51 @@ public static unsafe double DotAndDot(this sbyte[] rowVector, float[,] matrix, f #endif double result = 0; - fixed (sbyte* r = rowVector) - fixed (float* a = matrix) - fixed (float* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (sbyte* r = rowVector) + fixed (float* a = matrix) + fixed (float* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + sbyte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -31854,7 +32169,7 @@ public static double DotAndDot(this sbyte[] rowVector, float[][] matrix, double[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this sbyte[] rowVector, float[,] matrix, double[] columnVector) + public static double DotAndDot(this sbyte[] rowVector, float[,] matrix, double[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -31864,48 +32179,51 @@ public static unsafe double DotAndDot(this sbyte[] rowVector, float[,] matrix, d #endif double result = 0; - fixed (sbyte* r = rowVector) - fixed (float* a = matrix) - fixed (double* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (sbyte* r = rowVector) + fixed (float* a = matrix) + fixed (double* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + sbyte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -31961,7 +32279,7 @@ public static double DotAndDot(this sbyte[] rowVector, float[][] matrix, int[] c #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double DotAndDot(this sbyte[] rowVector, float[,] matrix, int[] columnVector) + public static double DotAndDot(this sbyte[] rowVector, float[,] matrix, int[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -31971,48 +32289,51 @@ public static unsafe double DotAndDot(this sbyte[] rowVector, float[,] matrix, i #endif double result = 0; - fixed (sbyte* r = rowVector) - fixed (float* a = matrix) - fixed (int* c = columnVector) - { - float* pa1 = a; - float* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*pc)); - sum2 += (double)((double)(*pa2++) * (double)(*pc)); - pc++; - } - - result += (double)((double)(*pr++) * sum1); - result += (double)((double)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (sbyte* r = rowVector) + fixed (float* a = matrix) + fixed (int* c = columnVector) + { + float* pa1 = a; + float* pa2 = a + cols; + sbyte* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*pc)); + sum2 += (double)((double)(*pa2++) * (double)(*pc)); + pc++; + } + + result += (double)((double)(*pr++) * sum1); + result += (double)((double)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* pc = c; - result += (double)((double)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*pc++)); + result += (double)((double)(*pr++) * sum); + } + } + } + return result; } @@ -32204,7 +32525,7 @@ public static int[][] Dot(this int[][] a, int[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this int[,] a, int[][] b, int[][] result) + public static int[][] Dot(this int[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -32218,21 +32539,24 @@ public static unsafe int[][] Dot(this int[,] a, int[][] b, int[][] result) var t = new int[K]; - fixed (int* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (int* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - int* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + int* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -32310,7 +32634,7 @@ public static int[] Dot(this int[][] matrix, int[] columnVector, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this int[,] matrix, int[] columnVector, int[] result) + public static int[] Dot(this int[,] matrix, int[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -32319,55 +32643,54 @@ public static unsafe int[] Dot(this int[,] matrix, int[] columnVector, int[] res if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (int* a = matrix) + fixed (int* x = columnVector) + fixed (int* r = result) + { + int* pa1 = a; + int* pa2 = a + cols; + int* pr = r; - fixed (int* a = matrix) - fixed (int* x = columnVector) - fixed (int* r = result) - { - int* pa1 = a; - int* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -32473,7 +32796,7 @@ public static int[] Dot(this int[] rowVector, int[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this int[][] a, int[,] b, int[][] result) + public static int[][] DotWithTransposed(this int[][] a, int[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -32483,19 +32806,22 @@ public static unsafe int[][] DotWithTransposed(this int[][] a, int[,] b, int[][] #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - int[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + int[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -32517,7 +32843,7 @@ public static unsafe int[][] DotWithTransposed(this int[][] a, int[,] b, int[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this int[,] a, int[][] b, int[][] result) + public static int[][] DotWithTransposed(this int[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -32527,20 +32853,22 @@ public static unsafe int[][] DotWithTransposed(this int[,] a, int[][] b, int[][] #endif int n = a.Rows(); - fixed (int* A = a) - for (int j = 0; j < b.Length; j++) - { - int* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (int* A = a) + for (int j = 0; j < b.Length; j++) + { + int* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -32686,7 +33014,7 @@ public static int[][] DotWithTransposed(this int[][] a, int[] columnVector, int[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this int[,] a, int[,] b, int[,] result) + public static int[,] TransposeAndDot(this int[,] a, int[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -32697,35 +33025,37 @@ public static int[][] DotWithTransposed(this int[][] a, int[] columnVector, int[ throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (int* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (int* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - int aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + int aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -33119,18 +33449,22 @@ public static int[][] DivideByDiagonal(this int[][] a, int[] diagonal, int[][] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this int[] a, int[] b, int[,] result) + public static int[,] Outer(this int[] a, int[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -33293,25 +33627,28 @@ public static int[][] Kronecker(this int[][] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this int[][] a, int[,] b, int[][] result) + public static int[][] Kronecker(this int[][] a, int[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -33365,18 +33702,22 @@ public static int[][] Kronecker(this int[,] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this int[] a, int[] b, int[] result) + public static int[] Kronecker(this int[] a, int[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -33570,7 +33911,7 @@ public static double[][] Dot(this int[][] a, int[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this int[,] a, int[][] b, double[][] result) + public static double[][] Dot(this int[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -33584,21 +33925,24 @@ public static unsafe double[][] Dot(this int[,] a, int[][] b, double[][] result) var t = new int[K]; - fixed (int* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (int* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - int* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + int* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -33676,7 +34020,7 @@ public static double[] Dot(this int[][] matrix, int[] columnVector, double[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this int[,] matrix, int[] columnVector, double[] result) + public static double[] Dot(this int[,] matrix, int[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -33685,55 +34029,54 @@ public static unsafe double[] Dot(this int[,] matrix, int[] columnVector, double if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (int* a = matrix) + fixed (int* x = columnVector) + fixed (double* r = result) + { + int* pa1 = a; + int* pa2 = a + cols; + double* pr = r; - fixed (int* a = matrix) - fixed (int* x = columnVector) - fixed (double* r = result) - { - int* pa1 = a; - int* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -33839,7 +34182,7 @@ public static double[] Dot(this int[] rowVector, int[][] matrix, double[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this int[][] a, int[,] b, double[][] result) + public static double[][] DotWithTransposed(this int[][] a, int[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -33849,19 +34192,22 @@ public static unsafe double[][] DotWithTransposed(this int[][] a, int[,] b, doub #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - int[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + int[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -33883,7 +34229,7 @@ public static unsafe double[][] DotWithTransposed(this int[][] a, int[,] b, doub #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this int[,] a, int[][] b, double[][] result) + public static double[][] DotWithTransposed(this int[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -33893,20 +34239,22 @@ public static unsafe double[][] DotWithTransposed(this int[,] a, int[][] b, doub #endif int n = a.Rows(); - fixed (int* A = a) - for (int j = 0; j < b.Length; j++) - { - int* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (int* A = a) + for (int j = 0; j < b.Length; j++) + { + int* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -34052,7 +34400,7 @@ public static double[][] DotWithTransposed(this int[][] a, int[] columnVector, d #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this int[,] a, int[,] b, double[,] result) + public static double[,] TransposeAndDot(this int[,] a, int[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -34063,35 +34411,37 @@ public static double[][] DotWithTransposed(this int[][] a, int[] columnVector, d throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (int* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (int* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - int aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + int aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -34485,18 +34835,22 @@ public static double[][] DivideByDiagonal(this int[][] a, int[] diagonal, double #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this int[] a, int[] b, double[,] result) + public static double[,] Outer(this int[] a, int[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -34659,25 +35013,28 @@ public static double[][] Kronecker(this int[][] a, int[][] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this int[][] a, int[,] b, double[][] result) + public static double[][] Kronecker(this int[][] a, int[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -34731,18 +35088,22 @@ public static double[][] Kronecker(this int[,] a, int[][] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this int[] a, int[] b, double[] result) + public static double[] Kronecker(this int[] a, int[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -34936,7 +35297,7 @@ public static int[][] Dot(this int[][] a, double[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this int[,] a, double[][] b, int[][] result) + public static int[][] Dot(this int[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -34950,21 +35311,24 @@ public static unsafe int[][] Dot(this int[,] a, double[][] b, int[][] result) var t = new double[K]; - fixed (int* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (int* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - int* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + int* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -35042,7 +35406,7 @@ public static int[] Dot(this int[][] matrix, double[] columnVector, int[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this int[,] matrix, double[] columnVector, int[] result) + public static int[] Dot(this int[,] matrix, double[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -35051,55 +35415,54 @@ public static unsafe int[] Dot(this int[,] matrix, double[] columnVector, int[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (int* a = matrix) + fixed (double* x = columnVector) + fixed (int* r = result) + { + int* pa1 = a; + int* pa2 = a + cols; + int* pr = r; - fixed (int* a = matrix) - fixed (double* x = columnVector) - fixed (int* r = result) - { - int* pa1 = a; - int* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -35205,7 +35568,7 @@ public static int[] Dot(this int[] rowVector, double[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this int[][] a, double[,] b, int[][] result) + public static int[][] DotWithTransposed(this int[][] a, double[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -35215,19 +35578,22 @@ public static unsafe int[][] DotWithTransposed(this int[][] a, double[,] b, int[ #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - int[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + int[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -35249,7 +35615,7 @@ public static unsafe int[][] DotWithTransposed(this int[][] a, double[,] b, int[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this int[,] a, double[][] b, int[][] result) + public static int[][] DotWithTransposed(this int[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -35259,20 +35625,22 @@ public static unsafe int[][] DotWithTransposed(this int[,] a, double[][] b, int[ #endif int n = a.Rows(); - fixed (int* A = a) - for (int j = 0; j < b.Length; j++) - { - int* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (int* A = a) + for (int j = 0; j < b.Length; j++) + { + int* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -35418,7 +35786,7 @@ public static int[][] DotWithTransposed(this int[][] a, double[] columnVector, i #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this int[,] a, double[,] b, int[,] result) + public static int[,] TransposeAndDot(this int[,] a, double[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -35429,35 +35797,37 @@ public static int[][] DotWithTransposed(this int[][] a, double[] columnVector, i throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (double* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (double* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - int aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + int aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -35851,18 +36221,22 @@ public static int[][] DivideByDiagonal(this int[][] a, double[] diagonal, int[][ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this int[] a, double[] b, int[,] result) + public static int[,] Outer(this int[] a, double[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -36025,25 +36399,28 @@ public static int[][] Kronecker(this int[][] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this int[][] a, double[,] b, int[][] result) + public static int[][] Kronecker(this int[][] a, double[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -36097,18 +36474,22 @@ public static int[][] Kronecker(this int[,] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this int[] a, double[] b, int[] result) + public static int[] Kronecker(this int[] a, double[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -36302,7 +36683,7 @@ public static double[][] Dot(this int[][] a, double[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this int[,] a, double[][] b, double[][] result) + public static double[][] Dot(this int[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -36316,21 +36697,24 @@ public static unsafe double[][] Dot(this int[,] a, double[][] b, double[][] resu var t = new double[K]; - fixed (int* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (int* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - int* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + int* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -36408,7 +36792,7 @@ public static double[] Dot(this int[][] matrix, double[] columnVector, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this int[,] matrix, double[] columnVector, double[] result) + public static double[] Dot(this int[,] matrix, double[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -36417,55 +36801,54 @@ public static unsafe double[] Dot(this int[,] matrix, double[] columnVector, dou if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (int* a = matrix) + fixed (double* x = columnVector) + fixed (double* r = result) + { + int* pa1 = a; + int* pa2 = a + cols; + double* pr = r; - fixed (int* a = matrix) - fixed (double* x = columnVector) - fixed (double* r = result) - { - int* pa1 = a; - int* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -36571,7 +36954,7 @@ public static double[] Dot(this int[] rowVector, double[][] matrix, double[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this int[][] a, double[,] b, double[][] result) + public static double[][] DotWithTransposed(this int[][] a, double[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -36581,19 +36964,22 @@ public static unsafe double[][] DotWithTransposed(this int[][] a, double[,] b, d #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - int[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + int[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -36615,7 +37001,7 @@ public static unsafe double[][] DotWithTransposed(this int[][] a, double[,] b, d #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this int[,] a, double[][] b, double[][] result) + public static double[][] DotWithTransposed(this int[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -36625,20 +37011,22 @@ public static unsafe double[][] DotWithTransposed(this int[,] a, double[][] b, d #endif int n = a.Rows(); - fixed (int* A = a) - for (int j = 0; j < b.Length; j++) - { - int* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (int* A = a) + for (int j = 0; j < b.Length; j++) + { + int* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -36784,7 +37172,7 @@ public static double[][] DotWithTransposed(this int[][] a, double[] columnVector #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this int[,] a, double[,] b, double[,] result) + public static double[,] TransposeAndDot(this int[,] a, double[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -36795,35 +37183,37 @@ public static double[][] DotWithTransposed(this int[][] a, double[] columnVector throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (double* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (double* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - int aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + int aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -37217,18 +37607,22 @@ public static double[][] DivideByDiagonal(this int[][] a, double[] diagonal, dou #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this int[] a, double[] b, double[,] result) + public static double[,] Outer(this int[] a, double[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -37391,25 +37785,28 @@ public static double[][] Kronecker(this int[][] a, double[][] b, double[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this int[][] a, double[,] b, double[][] result) + public static double[][] Kronecker(this int[][] a, double[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -37463,18 +37860,22 @@ public static double[][] Kronecker(this int[,] a, double[][] b, double[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this int[] a, double[] b, double[] result) + public static double[] Kronecker(this int[] a, double[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -37668,7 +38069,7 @@ public static int[][] Dot(this int[][] a, float[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this int[,] a, float[][] b, int[][] result) + public static int[][] Dot(this int[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -37682,21 +38083,24 @@ public static unsafe int[][] Dot(this int[,] a, float[][] b, int[][] result) var t = new float[K]; - fixed (int* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (int* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - int* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (int)s; - } - } + int* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -37774,7 +38178,7 @@ public static int[] Dot(this int[][] matrix, float[] columnVector, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this int[,] matrix, float[] columnVector, int[] result) + public static int[] Dot(this int[,] matrix, float[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -37783,55 +38187,54 @@ public static unsafe int[] Dot(this int[,] matrix, float[] columnVector, int[] r if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (int* a = matrix) + fixed (float* x = columnVector) + fixed (int* r = result) + { + int* pa1 = a; + int* pa2 = a + cols; + int* pr = r; - fixed (int* a = matrix) - fixed (float* x = columnVector) - fixed (int* r = result) - { - int* pa1 = a; - int* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -37937,7 +38340,7 @@ public static int[] Dot(this int[] rowVector, float[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this int[][] a, float[,] b, int[][] result) + public static int[][] DotWithTransposed(this int[][] a, float[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -37947,19 +38350,22 @@ public static unsafe int[][] DotWithTransposed(this int[][] a, float[,] b, int[] #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - int[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + int[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -37981,7 +38387,7 @@ public static unsafe int[][] DotWithTransposed(this int[][] a, float[,] b, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this int[,] a, float[][] b, int[][] result) + public static int[][] DotWithTransposed(this int[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -37991,20 +38397,22 @@ public static unsafe int[][] DotWithTransposed(this int[,] a, float[][] b, int[] #endif int n = a.Rows(); - fixed (int* A = a) - for (int j = 0; j < b.Length; j++) - { - int* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (int* A = a) + for (int j = 0; j < b.Length; j++) + { + int* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -38150,7 +38558,7 @@ public static int[][] DotWithTransposed(this int[][] a, float[] columnVector, in #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this int[,] a, float[,] b, int[,] result) + public static int[,] TransposeAndDot(this int[,] a, float[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -38161,35 +38569,37 @@ public static int[][] DotWithTransposed(this int[][] a, float[] columnVector, in throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - int aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + int aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -38583,18 +38993,22 @@ public static int[][] DivideByDiagonal(this int[][] a, float[] diagonal, int[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this int[] a, float[] b, int[,] result) + public static int[,] Outer(this int[] a, float[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -38757,25 +39171,28 @@ public static int[][] Kronecker(this int[][] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this int[][] a, float[,] b, int[][] result) + public static int[][] Kronecker(this int[][] a, float[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); + } + } + return result; } @@ -38829,18 +39246,22 @@ public static int[][] Kronecker(this int[,] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this int[] a, float[] b, int[] result) + public static int[] Kronecker(this int[] a, float[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -39034,7 +39455,7 @@ public static float[][] Dot(this int[][] a, float[,] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Dot(this int[,] a, float[][] b, float[][] result) + public static float[][] Dot(this int[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -39048,21 +39469,24 @@ public static unsafe float[][] Dot(this int[,] a, float[][] b, float[][] result) var t = new float[K]; - fixed (int* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (int* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - int* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (float)s; - } - } + int* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (float)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -39140,7 +39564,7 @@ public static float[] Dot(this int[][] matrix, float[] columnVector, float[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Dot(this int[,] matrix, float[] columnVector, float[] result) + public static float[] Dot(this int[,] matrix, float[] columnVector, float[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -39149,55 +39573,54 @@ public static unsafe float[] Dot(this int[,] matrix, float[] columnVector, float if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (int* a = matrix) + fixed (float* x = columnVector) + fixed (float* r = result) + { + int* pa1 = a; + int* pa2 = a + cols; + float* pr = r; - fixed (int* a = matrix) - fixed (float* x = columnVector) - fixed (float* r = result) - { - int* pa1 = a; - int* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (float)sum1; - *pr++ = (float)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (float)sum1; + *pr++ = (float)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (float)sum; - } - } + *pr = (float)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -39303,7 +39726,7 @@ public static float[] Dot(this int[] rowVector, float[][] matrix, float[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this int[][] a, float[,] b, float[][] result) + public static float[][] DotWithTransposed(this int[][] a, float[,] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -39313,19 +39736,22 @@ public static unsafe float[][] DotWithTransposed(this int[][] a, float[,] b, flo #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - int[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (float)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + int[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -39347,7 +39773,7 @@ public static unsafe float[][] DotWithTransposed(this int[][] a, float[,] b, flo #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this int[,] a, float[][] b, float[][] result) + public static float[][] DotWithTransposed(this int[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -39357,20 +39783,22 @@ public static unsafe float[][] DotWithTransposed(this int[,] a, float[][] b, flo #endif int n = a.Rows(); - fixed (int* A = a) - for (int j = 0; j < b.Length; j++) - { - int* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (float)sum; - } - } - + unsafe + { + fixed (int* A = a) + for (int j = 0; j < b.Length; j++) + { + int* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -39516,7 +39944,7 @@ public static float[][] DotWithTransposed(this int[][] a, float[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] TransposeAndDot(this int[,] a, float[,] b, float[,] result) + public static float[,] TransposeAndDot(this int[,] a, float[,] b, float[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -39527,35 +39955,37 @@ public static float[][] DotWithTransposed(this int[][] a, float[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (float* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + float* pr = R; - fixed (float* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - float* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - int aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (float)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + int aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (float)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -39949,18 +40379,22 @@ public static float[][] DivideByDiagonal(this int[][] a, float[] diagonal, float #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] Outer(this int[] a, float[] b, float[,] result) + public static float[,] Outer(this int[] a, float[] b, float[,] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -40123,25 +40557,28 @@ public static float[][] Kronecker(this int[][] a, float[][] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Kronecker(this int[][] a, float[,] b, float[][] result) + public static float[][] Kronecker(this int[][] a, float[,] b, float[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); + } + } + return result; } @@ -40195,18 +40632,22 @@ public static float[][] Kronecker(this int[,] a, float[][] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Kronecker(this int[] a, float[] b, float[] result) + public static float[] Kronecker(this int[] a, float[] b, float[] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -40400,7 +40841,7 @@ public static double[][] Dot(this int[][] a, float[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this int[,] a, float[][] b, double[][] result) + public static double[][] Dot(this int[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -40414,21 +40855,24 @@ public static unsafe double[][] Dot(this int[,] a, float[][] b, double[][] resul var t = new float[K]; - fixed (int* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (int* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - int* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + int* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -40506,7 +40950,7 @@ public static double[] Dot(this int[][] matrix, float[] columnVector, double[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this int[,] matrix, float[] columnVector, double[] result) + public static double[] Dot(this int[,] matrix, float[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -40515,55 +40959,54 @@ public static unsafe double[] Dot(this int[,] matrix, float[] columnVector, doub if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (int* a = matrix) + fixed (float* x = columnVector) + fixed (double* r = result) + { + int* pa1 = a; + int* pa2 = a + cols; + double* pr = r; - fixed (int* a = matrix) - fixed (float* x = columnVector) - fixed (double* r = result) - { - int* pa1 = a; - int* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -40669,7 +41112,7 @@ public static double[] Dot(this int[] rowVector, float[][] matrix, double[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this int[][] a, float[,] b, double[][] result) + public static double[][] DotWithTransposed(this int[][] a, float[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -40679,19 +41122,22 @@ public static unsafe double[][] DotWithTransposed(this int[][] a, float[,] b, do #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - int[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + int[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -40713,7 +41159,7 @@ public static unsafe double[][] DotWithTransposed(this int[][] a, float[,] b, do #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this int[,] a, float[][] b, double[][] result) + public static double[][] DotWithTransposed(this int[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -40723,20 +41169,22 @@ public static unsafe double[][] DotWithTransposed(this int[,] a, float[][] b, do #endif int n = a.Rows(); - fixed (int* A = a) - for (int j = 0; j < b.Length; j++) - { - int* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (int* A = a) + for (int j = 0; j < b.Length; j++) + { + int* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -40882,7 +41330,7 @@ public static double[][] DotWithTransposed(this int[][] a, float[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this int[,] a, float[,] b, double[,] result) + public static double[,] TransposeAndDot(this int[,] a, float[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -40893,35 +41341,37 @@ public static double[][] DotWithTransposed(this int[][] a, float[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - int aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + int aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -41315,18 +41765,22 @@ public static double[][] DivideByDiagonal(this int[][] a, float[] diagonal, doub #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this int[] a, float[] b, double[,] result) + public static double[,] Outer(this int[] a, float[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -41489,25 +41943,28 @@ public static double[][] Kronecker(this int[][] a, float[][] b, double[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this int[][] a, float[,] b, double[][] result) + public static double[][] Kronecker(this int[][] a, float[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -41561,18 +42018,22 @@ public static double[][] Kronecker(this int[,] a, float[][] b, double[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this int[] a, float[] b, double[] result) + public static double[] Kronecker(this int[] a, float[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -41766,7 +42227,7 @@ public static double[][] Dot(this double[][] a, double[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this double[,] a, double[][] b, double[][] result) + public static double[][] Dot(this double[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -41780,21 +42241,24 @@ public static unsafe double[][] Dot(this double[,] a, double[][] b, double[][] r var t = new double[K]; - fixed (double* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (double* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - double* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + double* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -41872,7 +42336,7 @@ public static double[] Dot(this double[][] matrix, double[] columnVector, double #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this double[,] matrix, double[] columnVector, double[] result) + public static double[] Dot(this double[,] matrix, double[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -41881,55 +42345,54 @@ public static unsafe double[] Dot(this double[,] matrix, double[] columnVector, if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (double* a = matrix) + fixed (double* x = columnVector) + fixed (double* r = result) + { + double* pa1 = a; + double* pa2 = a + cols; + double* pr = r; - fixed (double* a = matrix) - fixed (double* x = columnVector) - fixed (double* r = result) - { - double* pa1 = a; - double* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -42035,7 +42498,7 @@ public static double[] Dot(this double[] rowVector, double[][] matrix, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this double[][] a, double[,] b, double[][] result) + public static double[][] DotWithTransposed(this double[][] a, double[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -42045,19 +42508,22 @@ public static unsafe double[][] DotWithTransposed(this double[][] a, double[,] b #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - double[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + double[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -42079,7 +42545,7 @@ public static unsafe double[][] DotWithTransposed(this double[][] a, double[,] b #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this double[,] a, double[][] b, double[][] result) + public static double[][] DotWithTransposed(this double[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -42089,20 +42555,22 @@ public static unsafe double[][] DotWithTransposed(this double[,] a, double[][] b #endif int n = a.Rows(); - fixed (double* A = a) - for (int j = 0; j < b.Length; j++) - { - double* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (double* A = a) + for (int j = 0; j < b.Length; j++) + { + double* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -42248,7 +42716,7 @@ public static double[][] DotWithTransposed(this double[][] a, double[] columnVec #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this double[,] a, double[,] b, double[,] result) + public static double[,] TransposeAndDot(this double[,] a, double[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -42259,35 +42727,37 @@ public static double[][] DotWithTransposed(this double[][] a, double[] columnVec throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (double* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (double* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - double aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + double aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -42681,18 +43151,22 @@ public static double[][] DivideByDiagonal(this double[][] a, double[] diagonal, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this double[] a, double[] b, double[,] result) + public static double[,] Outer(this double[] a, double[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -42855,25 +43329,28 @@ public static double[][] Kronecker(this double[][] a, double[][] b, double[][] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this double[][] a, double[,] b, double[][] result) + public static double[][] Kronecker(this double[][] a, double[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -42927,18 +43404,22 @@ public static double[][] Kronecker(this double[,] a, double[][] b, double[][] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this double[] a, double[] b, double[] result) + public static double[] Kronecker(this double[] a, double[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -43132,7 +43613,7 @@ public static int[][] Dot(this double[][] a, double[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this double[,] a, double[][] b, int[][] result) + public static int[][] Dot(this double[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -43146,21 +43627,24 @@ public static unsafe int[][] Dot(this double[,] a, double[][] b, int[][] result) var t = new double[K]; - fixed (double* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (double* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - double* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + double* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -43238,7 +43722,7 @@ public static int[] Dot(this double[][] matrix, double[] columnVector, int[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this double[,] matrix, double[] columnVector, int[] result) + public static int[] Dot(this double[,] matrix, double[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -43247,55 +43731,54 @@ public static unsafe int[] Dot(this double[,] matrix, double[] columnVector, int if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (double* a = matrix) + fixed (double* x = columnVector) + fixed (int* r = result) + { + double* pa1 = a; + double* pa2 = a + cols; + int* pr = r; - fixed (double* a = matrix) - fixed (double* x = columnVector) - fixed (int* r = result) - { - double* pa1 = a; - double* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -43401,7 +43884,7 @@ public static int[] Dot(this double[] rowVector, double[][] matrix, int[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this double[][] a, double[,] b, int[][] result) + public static int[][] DotWithTransposed(this double[][] a, double[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -43411,19 +43894,22 @@ public static unsafe int[][] DotWithTransposed(this double[][] a, double[,] b, i #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - double[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + double[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -43445,7 +43931,7 @@ public static unsafe int[][] DotWithTransposed(this double[][] a, double[,] b, i #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this double[,] a, double[][] b, int[][] result) + public static int[][] DotWithTransposed(this double[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -43455,20 +43941,22 @@ public static unsafe int[][] DotWithTransposed(this double[,] a, double[][] b, i #endif int n = a.Rows(); - fixed (double* A = a) - for (int j = 0; j < b.Length; j++) - { - double* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (double* A = a) + for (int j = 0; j < b.Length; j++) + { + double* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -43614,7 +44102,7 @@ public static int[][] DotWithTransposed(this double[][] a, double[] columnVector #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this double[,] a, double[,] b, int[,] result) + public static int[,] TransposeAndDot(this double[,] a, double[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -43625,35 +44113,37 @@ public static int[][] DotWithTransposed(this double[][] a, double[] columnVector throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (double* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (double* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - double aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + double aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -44047,18 +44537,22 @@ public static int[][] DivideByDiagonal(this double[][] a, double[] diagonal, int #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this double[] a, double[] b, int[,] result) + public static int[,] Outer(this double[] a, double[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -44221,25 +44715,28 @@ public static int[][] Kronecker(this double[][] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this double[][] a, double[,] b, int[][] result) + public static int[][] Kronecker(this double[][] a, double[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -44293,18 +44790,22 @@ public static int[][] Kronecker(this double[,] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this double[] a, double[] b, int[] result) + public static int[] Kronecker(this double[] a, double[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -44498,7 +44999,7 @@ public static double[][] Dot(this double[][] a, int[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this double[,] a, int[][] b, double[][] result) + public static double[][] Dot(this double[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -44512,21 +45013,24 @@ public static unsafe double[][] Dot(this double[,] a, int[][] b, double[][] resu var t = new int[K]; - fixed (double* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (double* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - double* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + double* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -44604,7 +45108,7 @@ public static double[] Dot(this double[][] matrix, int[] columnVector, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this double[,] matrix, int[] columnVector, double[] result) + public static double[] Dot(this double[,] matrix, int[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -44613,55 +45117,54 @@ public static unsafe double[] Dot(this double[,] matrix, int[] columnVector, dou if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (double* a = matrix) + fixed (int* x = columnVector) + fixed (double* r = result) + { + double* pa1 = a; + double* pa2 = a + cols; + double* pr = r; - fixed (double* a = matrix) - fixed (int* x = columnVector) - fixed (double* r = result) - { - double* pa1 = a; - double* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -44767,7 +45270,7 @@ public static double[] Dot(this double[] rowVector, int[][] matrix, double[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this double[][] a, int[,] b, double[][] result) + public static double[][] DotWithTransposed(this double[][] a, int[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -44777,19 +45280,22 @@ public static unsafe double[][] DotWithTransposed(this double[][] a, int[,] b, d #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - double[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + double[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -44811,7 +45317,7 @@ public static unsafe double[][] DotWithTransposed(this double[][] a, int[,] b, d #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this double[,] a, int[][] b, double[][] result) + public static double[][] DotWithTransposed(this double[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -44821,20 +45327,22 @@ public static unsafe double[][] DotWithTransposed(this double[,] a, int[][] b, d #endif int n = a.Rows(); - fixed (double* A = a) - for (int j = 0; j < b.Length; j++) - { - double* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (double* A = a) + for (int j = 0; j < b.Length; j++) + { + double* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -44980,7 +45488,7 @@ public static double[][] DotWithTransposed(this double[][] a, int[] columnVector #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this double[,] a, int[,] b, double[,] result) + public static double[,] TransposeAndDot(this double[,] a, int[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -44991,35 +45499,37 @@ public static double[][] DotWithTransposed(this double[][] a, int[] columnVector throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (int* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (int* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - double aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + double aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -45413,18 +45923,22 @@ public static double[][] DivideByDiagonal(this double[][] a, int[] diagonal, dou #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this double[] a, int[] b, double[,] result) + public static double[,] Outer(this double[] a, int[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -45587,25 +46101,28 @@ public static double[][] Kronecker(this double[][] a, int[][] b, double[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this double[][] a, int[,] b, double[][] result) + public static double[][] Kronecker(this double[][] a, int[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -45659,18 +46176,22 @@ public static double[][] Kronecker(this double[,] a, int[][] b, double[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this double[] a, int[] b, double[] result) + public static double[] Kronecker(this double[] a, int[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -45864,7 +46385,7 @@ public static int[][] Dot(this double[][] a, int[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this double[,] a, int[][] b, int[][] result) + public static int[][] Dot(this double[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -45878,21 +46399,24 @@ public static unsafe int[][] Dot(this double[,] a, int[][] b, int[][] result) var t = new int[K]; - fixed (double* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (double* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - double* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + double* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -45970,7 +46494,7 @@ public static int[] Dot(this double[][] matrix, int[] columnVector, int[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this double[,] matrix, int[] columnVector, int[] result) + public static int[] Dot(this double[,] matrix, int[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -45979,55 +46503,54 @@ public static unsafe int[] Dot(this double[,] matrix, int[] columnVector, int[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (double* a = matrix) + fixed (int* x = columnVector) + fixed (int* r = result) + { + double* pa1 = a; + double* pa2 = a + cols; + int* pr = r; - fixed (double* a = matrix) - fixed (int* x = columnVector) - fixed (int* r = result) - { - double* pa1 = a; - double* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -46133,7 +46656,7 @@ public static int[] Dot(this double[] rowVector, int[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this double[][] a, int[,] b, int[][] result) + public static int[][] DotWithTransposed(this double[][] a, int[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -46143,19 +46666,22 @@ public static unsafe int[][] DotWithTransposed(this double[][] a, int[,] b, int[ #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - double[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + double[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -46177,7 +46703,7 @@ public static unsafe int[][] DotWithTransposed(this double[][] a, int[,] b, int[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this double[,] a, int[][] b, int[][] result) + public static int[][] DotWithTransposed(this double[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -46187,20 +46713,22 @@ public static unsafe int[][] DotWithTransposed(this double[,] a, int[][] b, int[ #endif int n = a.Rows(); - fixed (double* A = a) - for (int j = 0; j < b.Length; j++) - { - double* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (double* A = a) + for (int j = 0; j < b.Length; j++) + { + double* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -46346,7 +46874,7 @@ public static int[][] DotWithTransposed(this double[][] a, int[] columnVector, i #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this double[,] a, int[,] b, int[,] result) + public static int[,] TransposeAndDot(this double[,] a, int[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -46357,35 +46885,37 @@ public static int[][] DotWithTransposed(this double[][] a, int[] columnVector, i throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (int* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (int* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - double aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + double aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -46779,18 +47309,22 @@ public static int[][] DivideByDiagonal(this double[][] a, int[] diagonal, int[][ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this double[] a, int[] b, int[,] result) + public static int[,] Outer(this double[] a, int[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -46953,25 +47487,28 @@ public static int[][] Kronecker(this double[][] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this double[][] a, int[,] b, int[][] result) + public static int[][] Kronecker(this double[][] a, int[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -47025,18 +47562,22 @@ public static int[][] Kronecker(this double[,] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this double[] a, int[] b, int[] result) + public static int[] Kronecker(this double[] a, int[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -47230,7 +47771,7 @@ public static double[][] Dot(this double[][] a, float[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this double[,] a, float[][] b, double[][] result) + public static double[][] Dot(this double[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -47244,21 +47785,24 @@ public static unsafe double[][] Dot(this double[,] a, float[][] b, double[][] re var t = new float[K]; - fixed (double* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (double* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - double* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + double* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -47336,7 +47880,7 @@ public static double[] Dot(this double[][] matrix, float[] columnVector, double[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this double[,] matrix, float[] columnVector, double[] result) + public static double[] Dot(this double[,] matrix, float[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -47345,55 +47889,54 @@ public static unsafe double[] Dot(this double[,] matrix, float[] columnVector, d if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (double* a = matrix) + fixed (float* x = columnVector) + fixed (double* r = result) + { + double* pa1 = a; + double* pa2 = a + cols; + double* pr = r; - fixed (double* a = matrix) - fixed (float* x = columnVector) - fixed (double* r = result) - { - double* pa1 = a; - double* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -47499,7 +48042,7 @@ public static double[] Dot(this double[] rowVector, float[][] matrix, double[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this double[][] a, float[,] b, double[][] result) + public static double[][] DotWithTransposed(this double[][] a, float[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -47509,19 +48052,22 @@ public static unsafe double[][] DotWithTransposed(this double[][] a, float[,] b, #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - double[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + double[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -47543,7 +48089,7 @@ public static unsafe double[][] DotWithTransposed(this double[][] a, float[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this double[,] a, float[][] b, double[][] result) + public static double[][] DotWithTransposed(this double[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -47553,20 +48099,22 @@ public static unsafe double[][] DotWithTransposed(this double[,] a, float[][] b, #endif int n = a.Rows(); - fixed (double* A = a) - for (int j = 0; j < b.Length; j++) - { - double* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (double* A = a) + for (int j = 0; j < b.Length; j++) + { + double* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -47712,7 +48260,7 @@ public static double[][] DotWithTransposed(this double[][] a, float[] columnVect #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this double[,] a, float[,] b, double[,] result) + public static double[,] TransposeAndDot(this double[,] a, float[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -47723,35 +48271,37 @@ public static double[][] DotWithTransposed(this double[][] a, float[] columnVect throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - double aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + double aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -48145,18 +48695,22 @@ public static double[][] DivideByDiagonal(this double[][] a, float[] diagonal, d #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this double[] a, float[] b, double[,] result) + public static double[,] Outer(this double[] a, float[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -48319,25 +48873,28 @@ public static double[][] Kronecker(this double[][] a, float[][] b, double[][] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this double[][] a, float[,] b, double[][] result) + public static double[][] Kronecker(this double[][] a, float[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -48391,18 +48948,22 @@ public static double[][] Kronecker(this double[,] a, float[][] b, double[][] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this double[] a, float[] b, double[] result) + public static double[] Kronecker(this double[] a, float[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -48596,7 +49157,7 @@ public static float[][] Dot(this double[][] a, float[,] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Dot(this double[,] a, float[][] b, float[][] result) + public static float[][] Dot(this double[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -48610,21 +49171,24 @@ public static unsafe float[][] Dot(this double[,] a, float[][] b, float[][] resu var t = new float[K]; - fixed (double* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (double* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - double* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (float)s; - } - } + double* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (float)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -48702,7 +49266,7 @@ public static float[] Dot(this double[][] matrix, float[] columnVector, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Dot(this double[,] matrix, float[] columnVector, float[] result) + public static float[] Dot(this double[,] matrix, float[] columnVector, float[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -48711,55 +49275,54 @@ public static unsafe float[] Dot(this double[,] matrix, float[] columnVector, fl if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (double* a = matrix) + fixed (float* x = columnVector) + fixed (float* r = result) + { + double* pa1 = a; + double* pa2 = a + cols; + float* pr = r; - fixed (double* a = matrix) - fixed (float* x = columnVector) - fixed (float* r = result) - { - double* pa1 = a; - double* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (float)sum1; - *pr++ = (float)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (float)sum1; + *pr++ = (float)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (float)sum; - } - } + *pr = (float)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -48865,7 +49428,7 @@ public static float[] Dot(this double[] rowVector, float[][] matrix, float[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this double[][] a, float[,] b, float[][] result) + public static float[][] DotWithTransposed(this double[][] a, float[,] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -48875,19 +49438,22 @@ public static unsafe float[][] DotWithTransposed(this double[][] a, float[,] b, #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - double[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (float)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + double[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -48909,7 +49475,7 @@ public static unsafe float[][] DotWithTransposed(this double[][] a, float[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this double[,] a, float[][] b, float[][] result) + public static float[][] DotWithTransposed(this double[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -48919,20 +49485,22 @@ public static unsafe float[][] DotWithTransposed(this double[,] a, float[][] b, #endif int n = a.Rows(); - fixed (double* A = a) - for (int j = 0; j < b.Length; j++) - { - double* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (float)sum; - } - } - + unsafe + { + fixed (double* A = a) + for (int j = 0; j < b.Length; j++) + { + double* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -49078,7 +49646,7 @@ public static float[][] DotWithTransposed(this double[][] a, float[] columnVecto #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] TransposeAndDot(this double[,] a, float[,] b, float[,] result) + public static float[,] TransposeAndDot(this double[,] a, float[,] b, float[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -49089,35 +49657,37 @@ public static float[][] DotWithTransposed(this double[][] a, float[] columnVecto throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (float* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + float* pr = R; - fixed (float* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - float* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - double aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (float)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + double aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (float)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -49511,18 +50081,22 @@ public static float[][] DivideByDiagonal(this double[][] a, float[] diagonal, fl #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] Outer(this double[] a, float[] b, float[,] result) + public static float[,] Outer(this double[] a, float[] b, float[,] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -49685,25 +50259,28 @@ public static float[][] Kronecker(this double[][] a, float[][] b, float[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Kronecker(this double[][] a, float[,] b, float[][] result) + public static float[][] Kronecker(this double[][] a, float[,] b, float[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); + } + } + return result; } @@ -49757,18 +50334,22 @@ public static float[][] Kronecker(this double[,] a, float[][] b, float[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Kronecker(this double[] a, float[] b, float[] result) + public static float[] Kronecker(this double[] a, float[] b, float[] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -49962,7 +50543,7 @@ public static int[][] Dot(this double[][] a, float[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this double[,] a, float[][] b, int[][] result) + public static int[][] Dot(this double[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -49976,21 +50557,24 @@ public static unsafe int[][] Dot(this double[,] a, float[][] b, int[][] result) var t = new float[K]; - fixed (double* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (double* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - double* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (int)s; - } - } + double* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -50068,7 +50652,7 @@ public static int[] Dot(this double[][] matrix, float[] columnVector, int[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this double[,] matrix, float[] columnVector, int[] result) + public static int[] Dot(this double[,] matrix, float[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -50077,55 +50661,54 @@ public static unsafe int[] Dot(this double[,] matrix, float[] columnVector, int[ if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (double* a = matrix) + fixed (float* x = columnVector) + fixed (int* r = result) + { + double* pa1 = a; + double* pa2 = a + cols; + int* pr = r; - fixed (double* a = matrix) - fixed (float* x = columnVector) - fixed (int* r = result) - { - double* pa1 = a; - double* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -50231,7 +50814,7 @@ public static int[] Dot(this double[] rowVector, float[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this double[][] a, float[,] b, int[][] result) + public static int[][] DotWithTransposed(this double[][] a, float[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -50241,19 +50824,22 @@ public static unsafe int[][] DotWithTransposed(this double[][] a, float[,] b, in #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - double[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + double[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -50275,7 +50861,7 @@ public static unsafe int[][] DotWithTransposed(this double[][] a, float[,] b, in #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this double[,] a, float[][] b, int[][] result) + public static int[][] DotWithTransposed(this double[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -50285,20 +50871,22 @@ public static unsafe int[][] DotWithTransposed(this double[,] a, float[][] b, in #endif int n = a.Rows(); - fixed (double* A = a) - for (int j = 0; j < b.Length; j++) - { - double* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (double* A = a) + for (int j = 0; j < b.Length; j++) + { + double* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -50444,7 +51032,7 @@ public static int[][] DotWithTransposed(this double[][] a, float[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this double[,] a, float[,] b, int[,] result) + public static int[,] TransposeAndDot(this double[,] a, float[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -50455,35 +51043,37 @@ public static int[][] DotWithTransposed(this double[][] a, float[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - double aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + double aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -50877,18 +51467,22 @@ public static int[][] DivideByDiagonal(this double[][] a, float[] diagonal, int[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this double[] a, float[] b, int[,] result) + public static int[,] Outer(this double[] a, float[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -51051,25 +51645,28 @@ public static int[][] Kronecker(this double[][] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this double[][] a, float[,] b, int[][] result) + public static int[][] Kronecker(this double[][] a, float[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); + } + } + return result; } @@ -51123,18 +51720,22 @@ public static int[][] Kronecker(this double[,] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this double[] a, float[] b, int[] result) + public static int[] Kronecker(this double[] a, float[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -51328,7 +51929,7 @@ public static float[][] Dot(this float[][] a, float[,] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Dot(this float[,] a, float[][] b, float[][] result) + public static float[][] Dot(this float[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -51342,21 +51943,24 @@ public static unsafe float[][] Dot(this float[,] a, float[][] b, float[][] resul var t = new float[K]; - fixed (float* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (float* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - float* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (float)s; - } - } + float* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (float)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -51434,7 +52038,7 @@ public static float[] Dot(this float[][] matrix, float[] columnVector, float[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Dot(this float[,] matrix, float[] columnVector, float[] result) + public static float[] Dot(this float[,] matrix, float[] columnVector, float[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -51443,55 +52047,54 @@ public static unsafe float[] Dot(this float[,] matrix, float[] columnVector, flo if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (float* a = matrix) + fixed (float* x = columnVector) + fixed (float* r = result) + { + float* pa1 = a; + float* pa2 = a + cols; + float* pr = r; - fixed (float* a = matrix) - fixed (float* x = columnVector) - fixed (float* r = result) - { - float* pa1 = a; - float* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (float)sum1; - *pr++ = (float)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (float)sum1; + *pr++ = (float)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (float)sum; - } - } + *pr = (float)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -51597,7 +52200,7 @@ public static float[] Dot(this float[] rowVector, float[][] matrix, float[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this float[][] a, float[,] b, float[][] result) + public static float[][] DotWithTransposed(this float[][] a, float[,] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -51607,19 +52210,22 @@ public static unsafe float[][] DotWithTransposed(this float[][] a, float[,] b, f #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - float[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (float)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + float[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -51641,7 +52247,7 @@ public static unsafe float[][] DotWithTransposed(this float[][] a, float[,] b, f #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this float[,] a, float[][] b, float[][] result) + public static float[][] DotWithTransposed(this float[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -51651,20 +52257,22 @@ public static unsafe float[][] DotWithTransposed(this float[,] a, float[][] b, f #endif int n = a.Rows(); - fixed (float* A = a) - for (int j = 0; j < b.Length; j++) - { - float* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (float)sum; - } - } - + unsafe + { + fixed (float* A = a) + for (int j = 0; j < b.Length; j++) + { + float* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -51810,7 +52418,7 @@ public static float[][] DotWithTransposed(this float[][] a, float[] columnVector #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] TransposeAndDot(this float[,] a, float[,] b, float[,] result) + public static float[,] TransposeAndDot(this float[,] a, float[,] b, float[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -51821,35 +52429,37 @@ public static float[][] DotWithTransposed(this float[][] a, float[] columnVector throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (float* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + float* pr = R; - fixed (float* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - float* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - float aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (float)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + float aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (float)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -52243,18 +52853,22 @@ public static float[][] DivideByDiagonal(this float[][] a, float[] diagonal, flo #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] Outer(this float[] a, float[] b, float[,] result) + public static float[,] Outer(this float[] a, float[] b, float[,] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -52417,25 +53031,28 @@ public static float[][] Kronecker(this float[][] a, float[][] b, float[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Kronecker(this float[][] a, float[,] b, float[][] result) + public static float[][] Kronecker(this float[][] a, float[,] b, float[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); + } + } + return result; } @@ -52489,18 +53106,22 @@ public static float[][] Kronecker(this float[,] a, float[][] b, float[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Kronecker(this float[] a, float[] b, float[] result) + public static float[] Kronecker(this float[] a, float[] b, float[] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -52694,7 +53315,7 @@ public static double[][] Dot(this float[][] a, float[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this float[,] a, float[][] b, double[][] result) + public static double[][] Dot(this float[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -52708,21 +53329,24 @@ public static unsafe double[][] Dot(this float[,] a, float[][] b, double[][] res var t = new float[K]; - fixed (float* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (float* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - float* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + float* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -52800,7 +53424,7 @@ public static double[] Dot(this float[][] matrix, float[] columnVector, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this float[,] matrix, float[] columnVector, double[] result) + public static double[] Dot(this float[,] matrix, float[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -52809,55 +53433,54 @@ public static unsafe double[] Dot(this float[,] matrix, float[] columnVector, do if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (float* a = matrix) + fixed (float* x = columnVector) + fixed (double* r = result) + { + float* pa1 = a; + float* pa2 = a + cols; + double* pr = r; - fixed (float* a = matrix) - fixed (float* x = columnVector) - fixed (double* r = result) - { - float* pa1 = a; - float* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -52963,7 +53586,7 @@ public static double[] Dot(this float[] rowVector, float[][] matrix, double[] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this float[][] a, float[,] b, double[][] result) + public static double[][] DotWithTransposed(this float[][] a, float[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -52973,19 +53596,22 @@ public static unsafe double[][] DotWithTransposed(this float[][] a, float[,] b, #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - float[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + float[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -53007,7 +53633,7 @@ public static unsafe double[][] DotWithTransposed(this float[][] a, float[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this float[,] a, float[][] b, double[][] result) + public static double[][] DotWithTransposed(this float[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -53017,20 +53643,22 @@ public static unsafe double[][] DotWithTransposed(this float[,] a, float[][] b, #endif int n = a.Rows(); - fixed (float* A = a) - for (int j = 0; j < b.Length; j++) - { - float* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (float* A = a) + for (int j = 0; j < b.Length; j++) + { + float* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -53176,7 +53804,7 @@ public static double[][] DotWithTransposed(this float[][] a, float[] columnVecto #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this float[,] a, float[,] b, double[,] result) + public static double[,] TransposeAndDot(this float[,] a, float[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -53187,35 +53815,37 @@ public static double[][] DotWithTransposed(this float[][] a, float[] columnVecto throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - float aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + float aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -53609,18 +54239,22 @@ public static double[][] DivideByDiagonal(this float[][] a, float[] diagonal, do #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this float[] a, float[] b, double[,] result) + public static double[,] Outer(this float[] a, float[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -53783,25 +54417,28 @@ public static double[][] Kronecker(this float[][] a, float[][] b, double[][] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this float[][] a, float[,] b, double[][] result) + public static double[][] Kronecker(this float[][] a, float[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -53855,18 +54492,22 @@ public static double[][] Kronecker(this float[,] a, float[][] b, double[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this float[] a, float[] b, double[] result) + public static double[] Kronecker(this float[] a, float[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -54060,7 +54701,7 @@ public static int[][] Dot(this float[][] a, float[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this float[,] a, float[][] b, int[][] result) + public static int[][] Dot(this float[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -54074,21 +54715,24 @@ public static unsafe int[][] Dot(this float[,] a, float[][] b, int[][] result) var t = new float[K]; - fixed (float* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (float* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - float* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (int)s; - } - } + float* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -54166,7 +54810,7 @@ public static int[] Dot(this float[][] matrix, float[] columnVector, int[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this float[,] matrix, float[] columnVector, int[] result) + public static int[] Dot(this float[,] matrix, float[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -54175,55 +54819,54 @@ public static unsafe int[] Dot(this float[,] matrix, float[] columnVector, int[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (float* a = matrix) + fixed (float* x = columnVector) + fixed (int* r = result) + { + float* pa1 = a; + float* pa2 = a + cols; + int* pr = r; - fixed (float* a = matrix) - fixed (float* x = columnVector) - fixed (int* r = result) - { - float* pa1 = a; - float* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -54329,7 +54972,7 @@ public static int[] Dot(this float[] rowVector, float[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this float[][] a, float[,] b, int[][] result) + public static int[][] DotWithTransposed(this float[][] a, float[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -54339,19 +54982,22 @@ public static unsafe int[][] DotWithTransposed(this float[][] a, float[,] b, int #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - float[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + float[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -54373,7 +55019,7 @@ public static unsafe int[][] DotWithTransposed(this float[][] a, float[,] b, int #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this float[,] a, float[][] b, int[][] result) + public static int[][] DotWithTransposed(this float[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -54383,20 +55029,22 @@ public static unsafe int[][] DotWithTransposed(this float[,] a, float[][] b, int #endif int n = a.Rows(); - fixed (float* A = a) - for (int j = 0; j < b.Length; j++) - { - float* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (float* A = a) + for (int j = 0; j < b.Length; j++) + { + float* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -54542,7 +55190,7 @@ public static int[][] DotWithTransposed(this float[][] a, float[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this float[,] a, float[,] b, int[,] result) + public static int[,] TransposeAndDot(this float[,] a, float[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -54553,35 +55201,37 @@ public static int[][] DotWithTransposed(this float[][] a, float[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - float aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + float aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -54975,18 +55625,22 @@ public static int[][] DivideByDiagonal(this float[][] a, float[] diagonal, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this float[] a, float[] b, int[,] result) + public static int[,] Outer(this float[] a, float[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -55149,25 +55803,28 @@ public static int[][] Kronecker(this float[][] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this float[][] a, float[,] b, int[][] result) + public static int[][] Kronecker(this float[][] a, float[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); + } + } + return result; } @@ -55221,18 +55878,22 @@ public static int[][] Kronecker(this float[,] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this float[] a, float[] b, int[] result) + public static int[] Kronecker(this float[] a, float[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -55426,7 +56087,7 @@ public static float[][] Dot(this float[][] a, double[,] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Dot(this float[,] a, double[][] b, float[][] result) + public static float[][] Dot(this float[,] a, double[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -55440,21 +56101,24 @@ public static unsafe float[][] Dot(this float[,] a, double[][] b, float[][] resu var t = new double[K]; - fixed (float* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (float* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - float* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (float)s; - } - } + float* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (float)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -55532,7 +56196,7 @@ public static float[] Dot(this float[][] matrix, double[] columnVector, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Dot(this float[,] matrix, double[] columnVector, float[] result) + public static float[] Dot(this float[,] matrix, double[] columnVector, float[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -55541,55 +56205,54 @@ public static unsafe float[] Dot(this float[,] matrix, double[] columnVector, fl if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (float* a = matrix) + fixed (double* x = columnVector) + fixed (float* r = result) + { + float* pa1 = a; + float* pa2 = a + cols; + float* pr = r; - fixed (float* a = matrix) - fixed (double* x = columnVector) - fixed (float* r = result) - { - float* pa1 = a; - float* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (float)sum1; - *pr++ = (float)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (float)sum1; + *pr++ = (float)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (float)sum; - } - } + *pr = (float)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -55695,7 +56358,7 @@ public static float[] Dot(this float[] rowVector, double[][] matrix, float[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this float[][] a, double[,] b, float[][] result) + public static float[][] DotWithTransposed(this float[][] a, double[,] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -55705,19 +56368,22 @@ public static unsafe float[][] DotWithTransposed(this float[][] a, double[,] b, #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - float[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (float)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + float[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -55739,7 +56405,7 @@ public static unsafe float[][] DotWithTransposed(this float[][] a, double[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this float[,] a, double[][] b, float[][] result) + public static float[][] DotWithTransposed(this float[,] a, double[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -55749,20 +56415,22 @@ public static unsafe float[][] DotWithTransposed(this float[,] a, double[][] b, #endif int n = a.Rows(); - fixed (float* A = a) - for (int j = 0; j < b.Length; j++) - { - float* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (float)sum; - } - } - + unsafe + { + fixed (float* A = a) + for (int j = 0; j < b.Length; j++) + { + float* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -55908,7 +56576,7 @@ public static float[][] DotWithTransposed(this float[][] a, double[] columnVecto #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] TransposeAndDot(this float[,] a, double[,] b, float[,] result) + public static float[,] TransposeAndDot(this float[,] a, double[,] b, float[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -55919,35 +56587,37 @@ public static float[][] DotWithTransposed(this float[][] a, double[] columnVecto throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (float* R = result) + fixed (double* B = b) + fixed (double* ptemp = new double[p]) + { + float* pr = R; - fixed (float* R = result) - fixed (double* B = b) - fixed (double* ptemp = new double[p]) - { - float* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - float aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (float)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + float aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (float)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -56341,18 +57011,22 @@ public static float[][] DivideByDiagonal(this float[][] a, double[] diagonal, fl #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] Outer(this float[] a, double[] b, float[,] result) + public static float[,] Outer(this float[] a, double[] b, float[,] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -56515,25 +57189,28 @@ public static float[][] Kronecker(this float[][] a, double[][] b, float[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Kronecker(this float[][] a, double[,] b, float[][] result) + public static float[][] Kronecker(this float[][] a, double[,] b, float[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); + } + } + return result; } @@ -56587,18 +57264,22 @@ public static float[][] Kronecker(this float[,] a, double[][] b, float[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Kronecker(this float[] a, double[] b, float[] result) + public static float[] Kronecker(this float[] a, double[] b, float[] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -56792,7 +57473,7 @@ public static double[][] Dot(this float[][] a, double[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this float[,] a, double[][] b, double[][] result) + public static double[][] Dot(this float[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -56806,21 +57487,24 @@ public static unsafe double[][] Dot(this float[,] a, double[][] b, double[][] re var t = new double[K]; - fixed (float* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (float* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - float* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + float* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -56898,7 +57582,7 @@ public static double[] Dot(this float[][] matrix, double[] columnVector, double[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this float[,] matrix, double[] columnVector, double[] result) + public static double[] Dot(this float[,] matrix, double[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -56907,55 +57591,54 @@ public static unsafe double[] Dot(this float[,] matrix, double[] columnVector, d if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (float* a = matrix) + fixed (double* x = columnVector) + fixed (double* r = result) + { + float* pa1 = a; + float* pa2 = a + cols; + double* pr = r; - fixed (float* a = matrix) - fixed (double* x = columnVector) - fixed (double* r = result) - { - float* pa1 = a; - float* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -57061,7 +57744,7 @@ public static double[] Dot(this float[] rowVector, double[][] matrix, double[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this float[][] a, double[,] b, double[][] result) + public static double[][] DotWithTransposed(this float[][] a, double[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -57071,19 +57754,22 @@ public static unsafe double[][] DotWithTransposed(this float[][] a, double[,] b, #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - float[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + float[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -57105,7 +57791,7 @@ public static unsafe double[][] DotWithTransposed(this float[][] a, double[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this float[,] a, double[][] b, double[][] result) + public static double[][] DotWithTransposed(this float[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -57115,20 +57801,22 @@ public static unsafe double[][] DotWithTransposed(this float[,] a, double[][] b, #endif int n = a.Rows(); - fixed (float* A = a) - for (int j = 0; j < b.Length; j++) - { - float* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (float* A = a) + for (int j = 0; j < b.Length; j++) + { + float* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -57274,7 +57962,7 @@ public static double[][] DotWithTransposed(this float[][] a, double[] columnVect #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this float[,] a, double[,] b, double[,] result) + public static double[,] TransposeAndDot(this float[,] a, double[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -57285,35 +57973,37 @@ public static double[][] DotWithTransposed(this float[][] a, double[] columnVect throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (double* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (double* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - float aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + float aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -57707,18 +58397,22 @@ public static double[][] DivideByDiagonal(this float[][] a, double[] diagonal, d #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this float[] a, double[] b, double[,] result) + public static double[,] Outer(this float[] a, double[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -57881,25 +58575,28 @@ public static double[][] Kronecker(this float[][] a, double[][] b, double[][] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this float[][] a, double[,] b, double[][] result) + public static double[][] Kronecker(this float[][] a, double[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -57953,18 +58650,22 @@ public static double[][] Kronecker(this float[,] a, double[][] b, double[][] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this float[] a, double[] b, double[] result) + public static double[] Kronecker(this float[] a, double[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -58158,7 +58859,7 @@ public static int[][] Dot(this float[][] a, double[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this float[,] a, double[][] b, int[][] result) + public static int[][] Dot(this float[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -58172,21 +58873,24 @@ public static unsafe int[][] Dot(this float[,] a, double[][] b, int[][] result) var t = new double[K]; - fixed (float* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (float* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - float* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (int)s; - } - } + float* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -58264,7 +58968,7 @@ public static int[] Dot(this float[][] matrix, double[] columnVector, int[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this float[,] matrix, double[] columnVector, int[] result) + public static int[] Dot(this float[,] matrix, double[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -58273,55 +58977,54 @@ public static unsafe int[] Dot(this float[,] matrix, double[] columnVector, int[ if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (float* a = matrix) + fixed (double* x = columnVector) + fixed (int* r = result) + { + float* pa1 = a; + float* pa2 = a + cols; + int* pr = r; - fixed (float* a = matrix) - fixed (double* x = columnVector) - fixed (int* r = result) - { - float* pa1 = a; - float* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -58427,7 +59130,7 @@ public static int[] Dot(this float[] rowVector, double[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this float[][] a, double[,] b, int[][] result) + public static int[][] DotWithTransposed(this float[][] a, double[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -58437,19 +59140,22 @@ public static unsafe int[][] DotWithTransposed(this float[][] a, double[,] b, in #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - float[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + float[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -58471,7 +59177,7 @@ public static unsafe int[][] DotWithTransposed(this float[][] a, double[,] b, in #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this float[,] a, double[][] b, int[][] result) + public static int[][] DotWithTransposed(this float[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -58481,20 +59187,22 @@ public static unsafe int[][] DotWithTransposed(this float[,] a, double[][] b, in #endif int n = a.Rows(); - fixed (float* A = a) - for (int j = 0; j < b.Length; j++) - { - float* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (float* A = a) + for (int j = 0; j < b.Length; j++) + { + float* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -58640,7 +59348,7 @@ public static int[][] DotWithTransposed(this float[][] a, double[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this float[,] a, double[,] b, int[,] result) + public static int[,] TransposeAndDot(this float[,] a, double[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -58651,35 +59359,37 @@ public static int[][] DotWithTransposed(this float[][] a, double[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (double* B = b) + fixed (double* ptemp = new double[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (double* B = b) - fixed (double* ptemp = new double[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - float aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + float aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -59073,18 +59783,22 @@ public static int[][] DivideByDiagonal(this float[][] a, double[] diagonal, int[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this float[] a, double[] b, int[,] result) + public static int[,] Outer(this float[] a, double[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -59247,25 +59961,28 @@ public static int[][] Kronecker(this float[][] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this float[][] a, double[,] b, int[][] result) + public static int[][] Kronecker(this float[][] a, double[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); + } + } + return result; } @@ -59319,18 +60036,22 @@ public static int[][] Kronecker(this float[,] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this float[] a, double[] b, int[] result) + public static int[] Kronecker(this float[] a, double[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -59524,7 +60245,7 @@ public static float[][] Dot(this float[][] a, int[,] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Dot(this float[,] a, int[][] b, float[][] result) + public static float[][] Dot(this float[,] a, int[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -59538,21 +60259,24 @@ public static unsafe float[][] Dot(this float[,] a, int[][] b, float[][] result) var t = new int[K]; - fixed (float* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (float* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - float* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (float)s; - } - } + float* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (float)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -59630,7 +60354,7 @@ public static float[] Dot(this float[][] matrix, int[] columnVector, float[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Dot(this float[,] matrix, int[] columnVector, float[] result) + public static float[] Dot(this float[,] matrix, int[] columnVector, float[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -59639,55 +60363,54 @@ public static unsafe float[] Dot(this float[,] matrix, int[] columnVector, float if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (float* a = matrix) + fixed (int* x = columnVector) + fixed (float* r = result) + { + float* pa1 = a; + float* pa2 = a + cols; + float* pr = r; - fixed (float* a = matrix) - fixed (int* x = columnVector) - fixed (float* r = result) - { - float* pa1 = a; - float* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (float)sum1; - *pr++ = (float)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (float)sum1; + *pr++ = (float)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (float)sum; - } - } + *pr = (float)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -59793,7 +60516,7 @@ public static float[] Dot(this float[] rowVector, int[][] matrix, float[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this float[][] a, int[,] b, float[][] result) + public static float[][] DotWithTransposed(this float[][] a, int[,] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -59803,19 +60526,22 @@ public static unsafe float[][] DotWithTransposed(this float[][] a, int[,] b, flo #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - float[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (float)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + float[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -59837,7 +60563,7 @@ public static unsafe float[][] DotWithTransposed(this float[][] a, int[,] b, flo #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this float[,] a, int[][] b, float[][] result) + public static float[][] DotWithTransposed(this float[,] a, int[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -59847,20 +60573,22 @@ public static unsafe float[][] DotWithTransposed(this float[,] a, int[][] b, flo #endif int n = a.Rows(); - fixed (float* A = a) - for (int j = 0; j < b.Length; j++) - { - float* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (float)sum; - } - } - + unsafe + { + fixed (float* A = a) + for (int j = 0; j < b.Length; j++) + { + float* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -60006,7 +60734,7 @@ public static float[][] DotWithTransposed(this float[][] a, int[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] TransposeAndDot(this float[,] a, int[,] b, float[,] result) + public static float[,] TransposeAndDot(this float[,] a, int[,] b, float[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -60017,35 +60745,37 @@ public static float[][] DotWithTransposed(this float[][] a, int[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (float* R = result) + fixed (int* B = b) + fixed (double* ptemp = new double[p]) + { + float* pr = R; - fixed (float* R = result) - fixed (int* B = b) - fixed (double* ptemp = new double[p]) - { - float* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - float aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (float)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + float aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (float)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -60439,18 +61169,22 @@ public static float[][] DivideByDiagonal(this float[][] a, int[] diagonal, float #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] Outer(this float[] a, int[] b, float[,] result) + public static float[,] Outer(this float[] a, int[] b, float[,] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -60613,25 +61347,28 @@ public static float[][] Kronecker(this float[][] a, int[][] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Kronecker(this float[][] a, int[,] b, float[][] result) + public static float[][] Kronecker(this float[][] a, int[,] b, float[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); + } + } + return result; } @@ -60685,18 +61422,22 @@ public static float[][] Kronecker(this float[,] a, int[][] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Kronecker(this float[] a, int[] b, float[] result) + public static float[] Kronecker(this float[] a, int[] b, float[] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -60890,7 +61631,7 @@ public static int[][] Dot(this float[][] a, int[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this float[,] a, int[][] b, int[][] result) + public static int[][] Dot(this float[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -60904,21 +61645,24 @@ public static unsafe int[][] Dot(this float[,] a, int[][] b, int[][] result) var t = new int[K]; - fixed (float* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (float* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - float* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (int)s; - } - } + float* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -60996,7 +61740,7 @@ public static int[] Dot(this float[][] matrix, int[] columnVector, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this float[,] matrix, int[] columnVector, int[] result) + public static int[] Dot(this float[,] matrix, int[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -61005,55 +61749,54 @@ public static unsafe int[] Dot(this float[,] matrix, int[] columnVector, int[] r if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (float* a = matrix) + fixed (int* x = columnVector) + fixed (int* r = result) + { + float* pa1 = a; + float* pa2 = a + cols; + int* pr = r; - fixed (float* a = matrix) - fixed (int* x = columnVector) - fixed (int* r = result) - { - float* pa1 = a; - float* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -61159,7 +61902,7 @@ public static int[] Dot(this float[] rowVector, int[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this float[][] a, int[,] b, int[][] result) + public static int[][] DotWithTransposed(this float[][] a, int[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -61169,19 +61912,22 @@ public static unsafe int[][] DotWithTransposed(this float[][] a, int[,] b, int[] #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - float[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + float[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -61203,7 +61949,7 @@ public static unsafe int[][] DotWithTransposed(this float[][] a, int[,] b, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this float[,] a, int[][] b, int[][] result) + public static int[][] DotWithTransposed(this float[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -61213,20 +61959,22 @@ public static unsafe int[][] DotWithTransposed(this float[,] a, int[][] b, int[] #endif int n = a.Rows(); - fixed (float* A = a) - for (int j = 0; j < b.Length; j++) - { - float* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (float* A = a) + for (int j = 0; j < b.Length; j++) + { + float* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -61372,7 +62120,7 @@ public static int[][] DotWithTransposed(this float[][] a, int[] columnVector, in #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this float[,] a, int[,] b, int[,] result) + public static int[,] TransposeAndDot(this float[,] a, int[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -61383,35 +62131,37 @@ public static int[][] DotWithTransposed(this float[][] a, int[] columnVector, in throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (int* B = b) + fixed (double* ptemp = new double[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (int* B = b) - fixed (double* ptemp = new double[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - float aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + float aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -61805,18 +62555,22 @@ public static int[][] DivideByDiagonal(this float[][] a, int[] diagonal, int[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this float[] a, int[] b, int[,] result) + public static int[,] Outer(this float[] a, int[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -61979,25 +62733,28 @@ public static int[][] Kronecker(this float[][] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this float[][] a, int[,] b, int[][] result) + public static int[][] Kronecker(this float[][] a, int[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); + } + } + return result; } @@ -62051,18 +62808,22 @@ public static int[][] Kronecker(this float[,] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this float[] a, int[] b, int[] result) + public static int[] Kronecker(this float[] a, int[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -62256,7 +63017,7 @@ public static double[][] Dot(this float[][] a, int[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this float[,] a, int[][] b, double[][] result) + public static double[][] Dot(this float[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -62270,21 +63031,24 @@ public static unsafe double[][] Dot(this float[,] a, int[][] b, double[][] resul var t = new int[K]; - fixed (float* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (float* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - float* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + float* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -62362,7 +63126,7 @@ public static double[] Dot(this float[][] matrix, int[] columnVector, double[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this float[,] matrix, int[] columnVector, double[] result) + public static double[] Dot(this float[,] matrix, int[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -62371,55 +63135,54 @@ public static unsafe double[] Dot(this float[,] matrix, int[] columnVector, doub if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (float* a = matrix) + fixed (int* x = columnVector) + fixed (double* r = result) + { + float* pa1 = a; + float* pa2 = a + cols; + double* pr = r; - fixed (float* a = matrix) - fixed (int* x = columnVector) - fixed (double* r = result) - { - float* pa1 = a; - float* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -62525,7 +63288,7 @@ public static double[] Dot(this float[] rowVector, int[][] matrix, double[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this float[][] a, int[,] b, double[][] result) + public static double[][] DotWithTransposed(this float[][] a, int[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -62535,19 +63298,22 @@ public static unsafe double[][] DotWithTransposed(this float[][] a, int[,] b, do #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - float[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + float[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -62569,7 +63335,7 @@ public static unsafe double[][] DotWithTransposed(this float[][] a, int[,] b, do #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this float[,] a, int[][] b, double[][] result) + public static double[][] DotWithTransposed(this float[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -62579,20 +63345,22 @@ public static unsafe double[][] DotWithTransposed(this float[,] a, int[][] b, do #endif int n = a.Rows(); - fixed (float* A = a) - for (int j = 0; j < b.Length; j++) - { - float* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (float* A = a) + for (int j = 0; j < b.Length; j++) + { + float* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -62738,7 +63506,7 @@ public static double[][] DotWithTransposed(this float[][] a, int[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this float[,] a, int[,] b, double[,] result) + public static double[,] TransposeAndDot(this float[,] a, int[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -62749,35 +63517,37 @@ public static double[][] DotWithTransposed(this float[][] a, int[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (int* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (int* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - float aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + float aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -63171,18 +63941,22 @@ public static double[][] DivideByDiagonal(this float[][] a, int[] diagonal, doub #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this float[] a, int[] b, double[,] result) + public static double[,] Outer(this float[] a, int[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -63345,25 +64119,28 @@ public static double[][] Kronecker(this float[][] a, int[][] b, double[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this float[][] a, int[,] b, double[][] result) + public static double[][] Kronecker(this float[][] a, int[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -63417,18 +64194,22 @@ public static double[][] Kronecker(this float[,] a, int[][] b, double[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this float[] a, int[] b, double[] result) + public static double[] Kronecker(this float[] a, int[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -63622,7 +64403,7 @@ public static long[][] Dot(this long[][] a, long[,] b, long[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] Dot(this long[,] a, long[][] b, long[][] result) + public static long[][] Dot(this long[,] a, long[][] b, long[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -63636,21 +64417,24 @@ public static unsafe long[][] Dot(this long[,] a, long[][] b, long[][] result) var t = new long[K]; - fixed (long* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (long* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - long* pa = A; - for (int i = 0; i < N; i++) - { - long s = (long)0; - for (int k = 0; k < t.Length; k++) - s += (long)((long)(*pa++) * (long)t[k]); - result[i][j] = (long)s; - } - } + long* pa = A; + for (int i = 0; i < N; i++) + { + long s = (long)0; + for (int k = 0; k < t.Length; k++) + s += (long)((long)(*pa++) * (long)t[k]); + result[i][j] = (long)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -63728,7 +64512,7 @@ public static long[] Dot(this long[][] matrix, long[] columnVector, long[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[] Dot(this long[,] matrix, long[] columnVector, long[] result) + public static long[] Dot(this long[,] matrix, long[] columnVector, long[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -63737,55 +64521,54 @@ public static unsafe long[] Dot(this long[,] matrix, long[] columnVector, long[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (long* a = matrix) + fixed (long* x = columnVector) + fixed (long* r = result) + { + long* pa1 = a; + long* pa2 = a + cols; + long* pr = r; - fixed (long* a = matrix) - fixed (long* x = columnVector) - fixed (long* r = result) - { - long* pa1 = a; - long* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - long sum1 = 0, sum2 = 0; - long* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (long)((long)(*pa1++) * (long)(*px)); - sum2 += (long)((long)(*pa2++) * (long)(*px)); - px++; - } - - *pr++ = (long)sum1; - *pr++ = (long)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + long sum1 = 0, sum2 = 0; + long* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (long)((long)(*pa1++) * (long)(*px)); + sum2 += (long)((long)(*pa2++) * (long)(*px)); + px++; + } + + *pr++ = (long)sum1; + *pr++ = (long)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - long sum = 0; - long* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + long sum = 0; + long* px = x; - for (int j = 0; j < cols; j++) - sum += (long)((long)(*pa1++) * (long)(*px++)); + for (int j = 0; j < cols; j++) + sum += (long)((long)(*pa1++) * (long)(*px++)); - *pr = (long)sum; - } - } + *pr = (long)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -63891,7 +64674,7 @@ public static long[] Dot(this long[] rowVector, long[][] matrix, long[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] DotWithTransposed(this long[][] a, long[,] b, long[][] result) + public static long[][] DotWithTransposed(this long[][] a, long[,] b, long[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -63901,19 +64684,22 @@ public static unsafe long[][] DotWithTransposed(this long[][] a, long[,] b, long #endif int n = b.Rows(); - fixed (long* B = b) - for (int i = 0; i < a.Length; i++) - { - long* pb = B; - long[] arow = a[i]; - for (int j = 0; j < n; j++) - { - long sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (long)((long)arow[k] * (long)(*pb++)); - result[i][j] = (long)sum; - } - } + unsafe + { + fixed (long* B = b) + for (int i = 0; i < a.Length; i++) + { + long* pb = B; + long[] arow = a[i]; + for (int j = 0; j < n; j++) + { + long sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (long)((long)arow[k] * (long)(*pb++)); + result[i][j] = (long)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -63935,7 +64721,7 @@ public static unsafe long[][] DotWithTransposed(this long[][] a, long[,] b, long #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] DotWithTransposed(this long[,] a, long[][] b, long[][] result) + public static long[][] DotWithTransposed(this long[,] a, long[][] b, long[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -63945,20 +64731,22 @@ public static unsafe long[][] DotWithTransposed(this long[,] a, long[][] b, long #endif int n = a.Rows(); - fixed (long* A = a) - for (int j = 0; j < b.Length; j++) - { - long* pa = A; - for (int i = 0; i < n; i++) - { - long sum = 0; - long[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (long)((long)(*pa++) * (long)brow[k]); - result[i][j] = (long)sum; - } - } - + unsafe + { + fixed (long* A = a) + for (int j = 0; j < b.Length; j++) + { + long* pa = A; + for (int i = 0; i < n; i++) + { + long sum = 0; + long[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (long)((long)(*pa++) * (long)brow[k]); + result[i][j] = (long)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -64104,7 +64892,7 @@ public static long[][] DotWithTransposed(this long[][] a, long[] columnVector, l #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[,] TransposeAndDot(this long[,] a, long[,] b, long[,] result) + public static long[,] TransposeAndDot(this long[,] a, long[,] b, long[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -64115,35 +64903,37 @@ public static long[][] DotWithTransposed(this long[][] a, long[] columnVector, l throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (long* R = result) + fixed (long* B = b) + fixed (long* ptemp = new long[p]) + { + long* pr = R; - fixed (long* R = result) - fixed (long* B = b) - fixed (long* ptemp = new long[p]) - { - long* pr = R; - - for (int i = 0; i < m; i++) - { - long* pt = ptemp; - long* pb = B; - - for (int k = 0; k < n; k++) - { - long aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (long)((long)aval * (long)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (long)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + long* pt = ptemp; + long* pb = B; + + for (int k = 0; k < n; k++) + { + long aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (long)((long)aval * (long)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (long)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -64537,18 +65327,22 @@ public static long[][] DivideByDiagonal(this long[][] a, long[] diagonal, long[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[,] Outer(this long[] a, long[] b, long[,] result) + public static long[,] Outer(this long[] a, long[] b, long[,] result) { - fixed (long* R = result) - { - long* pr = R; - for (int i = 0; i < a.Length; i++) - { - long x = (long)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (long)(x * (long)b[j]); - } - } + unsafe + { + fixed (long* R = result) + { + long* pr = R; + for (int i = 0; i < a.Length; i++) + { + long x = (long)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (long)(x * (long)b[j]); + } + } + } + return result; } @@ -64711,25 +65505,28 @@ public static long[][] Kronecker(this long[][] a, long[][] b, long[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] Kronecker(this long[][] a, long[,] b, long[][] result) + public static long[][] Kronecker(this long[][] a, long[,] b, long[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (long* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - long aval = (long)a[i][j]; - long* pb = B; + unsafe + { + fixed (long* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + long aval = (long)a[i][j]; + long* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (long)(aval * (long)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (long)(aval * (long)(*pb++)); + } + } + return result; } @@ -64783,18 +65580,22 @@ public static long[][] Kronecker(this long[,] a, long[][] b, long[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[] Kronecker(this long[] a, long[] b, long[] result) + public static long[] Kronecker(this long[] a, long[] b, long[] result) { - fixed (long* R = result) - { - long* pr = R; - for (int i = 0; i < a.Length; i++) - { - long x = (long)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (long)(x * (long)b[j]); - } - } + unsafe + { + fixed (long* R = result) + { + long* pr = R; + for (int i = 0; i < a.Length; i++) + { + long x = (long)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (long)(x * (long)b[j]); + } + } + } + return result; } @@ -64988,7 +65789,7 @@ public static double[][] Dot(this long[][] a, long[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this long[,] a, long[][] b, double[][] result) + public static double[][] Dot(this long[,] a, long[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -65002,21 +65803,24 @@ public static unsafe double[][] Dot(this long[,] a, long[][] b, double[][] resul var t = new long[K]; - fixed (long* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (long* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - long* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + long* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -65094,7 +65898,7 @@ public static double[] Dot(this long[][] matrix, long[] columnVector, double[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this long[,] matrix, long[] columnVector, double[] result) + public static double[] Dot(this long[,] matrix, long[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -65103,55 +65907,54 @@ public static unsafe double[] Dot(this long[,] matrix, long[] columnVector, doub if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (long* a = matrix) + fixed (long* x = columnVector) + fixed (double* r = result) + { + long* pa1 = a; + long* pa2 = a + cols; + double* pr = r; - fixed (long* a = matrix) - fixed (long* x = columnVector) - fixed (double* r = result) - { - long* pa1 = a; - long* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - long* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + long* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - long* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + long* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -65257,7 +66060,7 @@ public static double[] Dot(this long[] rowVector, long[][] matrix, double[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this long[][] a, long[,] b, double[][] result) + public static double[][] DotWithTransposed(this long[][] a, long[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -65267,19 +66070,22 @@ public static unsafe double[][] DotWithTransposed(this long[][] a, long[,] b, do #endif int n = b.Rows(); - fixed (long* B = b) - for (int i = 0; i < a.Length; i++) - { - long* pb = B; - long[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (long* B = b) + for (int i = 0; i < a.Length; i++) + { + long* pb = B; + long[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -65301,7 +66107,7 @@ public static unsafe double[][] DotWithTransposed(this long[][] a, long[,] b, do #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this long[,] a, long[][] b, double[][] result) + public static double[][] DotWithTransposed(this long[,] a, long[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -65311,20 +66117,22 @@ public static unsafe double[][] DotWithTransposed(this long[,] a, long[][] b, do #endif int n = a.Rows(); - fixed (long* A = a) - for (int j = 0; j < b.Length; j++) - { - long* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - long[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (long* A = a) + for (int j = 0; j < b.Length; j++) + { + long* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + long[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -65470,7 +66278,7 @@ public static double[][] DotWithTransposed(this long[][] a, long[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this long[,] a, long[,] b, double[,] result) + public static double[,] TransposeAndDot(this long[,] a, long[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -65481,35 +66289,37 @@ public static double[][] DotWithTransposed(this long[][] a, long[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (long* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (long* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - long* pb = B; - - for (int k = 0; k < n; k++) - { - long aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + long* pb = B; + + for (int k = 0; k < n; k++) + { + long aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -65903,18 +66713,22 @@ public static double[][] DivideByDiagonal(this long[][] a, long[] diagonal, doub #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this long[] a, long[] b, double[,] result) + public static double[,] Outer(this long[] a, long[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -66077,25 +66891,28 @@ public static double[][] Kronecker(this long[][] a, long[][] b, double[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this long[][] a, long[,] b, double[][] result) + public static double[][] Kronecker(this long[][] a, long[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (long* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - long* pb = B; + unsafe + { + fixed (long* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + long* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -66149,18 +66966,22 @@ public static double[][] Kronecker(this long[,] a, long[][] b, double[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this long[] a, long[] b, double[] result) + public static double[] Kronecker(this long[] a, long[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -66354,7 +67175,7 @@ public static int[][] Dot(this long[][] a, long[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this long[,] a, long[][] b, int[][] result) + public static int[][] Dot(this long[,] a, long[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -66368,21 +67189,24 @@ public static unsafe int[][] Dot(this long[,] a, long[][] b, int[][] result) var t = new long[K]; - fixed (long* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (long* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - long* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + long* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -66460,7 +67284,7 @@ public static int[] Dot(this long[][] matrix, long[] columnVector, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this long[,] matrix, long[] columnVector, int[] result) + public static int[] Dot(this long[,] matrix, long[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -66469,55 +67293,54 @@ public static unsafe int[] Dot(this long[,] matrix, long[] columnVector, int[] r if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (long* a = matrix) + fixed (long* x = columnVector) + fixed (int* r = result) + { + long* pa1 = a; + long* pa2 = a + cols; + int* pr = r; - fixed (long* a = matrix) - fixed (long* x = columnVector) - fixed (int* r = result) - { - long* pa1 = a; - long* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - long* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + long* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - long* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + long* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -66623,7 +67446,7 @@ public static int[] Dot(this long[] rowVector, long[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this long[][] a, long[,] b, int[][] result) + public static int[][] DotWithTransposed(this long[][] a, long[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -66633,19 +67456,22 @@ public static unsafe int[][] DotWithTransposed(this long[][] a, long[,] b, int[] #endif int n = b.Rows(); - fixed (long* B = b) - for (int i = 0; i < a.Length; i++) - { - long* pb = B; - long[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (long* B = b) + for (int i = 0; i < a.Length; i++) + { + long* pb = B; + long[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -66667,7 +67493,7 @@ public static unsafe int[][] DotWithTransposed(this long[][] a, long[,] b, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this long[,] a, long[][] b, int[][] result) + public static int[][] DotWithTransposed(this long[,] a, long[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -66677,20 +67503,22 @@ public static unsafe int[][] DotWithTransposed(this long[,] a, long[][] b, int[] #endif int n = a.Rows(); - fixed (long* A = a) - for (int j = 0; j < b.Length; j++) - { - long* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - long[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (long* A = a) + for (int j = 0; j < b.Length; j++) + { + long* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + long[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -66836,7 +67664,7 @@ public static int[][] DotWithTransposed(this long[][] a, long[] columnVector, in #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this long[,] a, long[,] b, int[,] result) + public static int[,] TransposeAndDot(this long[,] a, long[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -66847,35 +67675,37 @@ public static int[][] DotWithTransposed(this long[][] a, long[] columnVector, in throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (long* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (long* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - long* pb = B; - - for (int k = 0; k < n; k++) - { - long aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + long* pb = B; + + for (int k = 0; k < n; k++) + { + long aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -67269,18 +68099,22 @@ public static int[][] DivideByDiagonal(this long[][] a, long[] diagonal, int[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this long[] a, long[] b, int[,] result) + public static int[,] Outer(this long[] a, long[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -67443,25 +68277,28 @@ public static int[][] Kronecker(this long[][] a, long[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this long[][] a, long[,] b, int[][] result) + public static int[][] Kronecker(this long[][] a, long[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (long* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - long* pb = B; + unsafe + { + fixed (long* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + long* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -67515,18 +68352,22 @@ public static int[][] Kronecker(this long[,] a, long[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this long[] a, long[] b, int[] result) + public static int[] Kronecker(this long[] a, long[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -67720,7 +68561,7 @@ public static long[][] Dot(this long[][] a, double[,] b, long[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] Dot(this long[,] a, double[][] b, long[][] result) + public static long[][] Dot(this long[,] a, double[][] b, long[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -67734,21 +68575,24 @@ public static unsafe long[][] Dot(this long[,] a, double[][] b, long[][] result) var t = new double[K]; - fixed (long* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (long* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - long* pa = A; - for (int i = 0; i < N; i++) - { - long s = (long)0; - for (int k = 0; k < t.Length; k++) - s += (long)((long)(*pa++) * (long)t[k]); - result[i][j] = (long)s; - } - } + long* pa = A; + for (int i = 0; i < N; i++) + { + long s = (long)0; + for (int k = 0; k < t.Length; k++) + s += (long)((long)(*pa++) * (long)t[k]); + result[i][j] = (long)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -67826,7 +68670,7 @@ public static long[] Dot(this long[][] matrix, double[] columnVector, long[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[] Dot(this long[,] matrix, double[] columnVector, long[] result) + public static long[] Dot(this long[,] matrix, double[] columnVector, long[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -67835,55 +68679,54 @@ public static unsafe long[] Dot(this long[,] matrix, double[] columnVector, long if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (long* a = matrix) + fixed (double* x = columnVector) + fixed (long* r = result) + { + long* pa1 = a; + long* pa2 = a + cols; + long* pr = r; - fixed (long* a = matrix) - fixed (double* x = columnVector) - fixed (long* r = result) - { - long* pa1 = a; - long* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - long sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (long)((long)(*pa1++) * (long)(*px)); - sum2 += (long)((long)(*pa2++) * (long)(*px)); - px++; - } - - *pr++ = (long)sum1; - *pr++ = (long)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + long sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (long)((long)(*pa1++) * (long)(*px)); + sum2 += (long)((long)(*pa2++) * (long)(*px)); + px++; + } + + *pr++ = (long)sum1; + *pr++ = (long)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - long sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + long sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (long)((long)(*pa1++) * (long)(*px++)); + for (int j = 0; j < cols; j++) + sum += (long)((long)(*pa1++) * (long)(*px++)); - *pr = (long)sum; - } - } + *pr = (long)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -67989,7 +68832,7 @@ public static long[] Dot(this long[] rowVector, double[][] matrix, long[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] DotWithTransposed(this long[][] a, double[,] b, long[][] result) + public static long[][] DotWithTransposed(this long[][] a, double[,] b, long[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -67999,19 +68842,22 @@ public static unsafe long[][] DotWithTransposed(this long[][] a, double[,] b, lo #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - long[] arow = a[i]; - for (int j = 0; j < n; j++) - { - long sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (long)((long)arow[k] * (long)(*pb++)); - result[i][j] = (long)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + long[] arow = a[i]; + for (int j = 0; j < n; j++) + { + long sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (long)((long)arow[k] * (long)(*pb++)); + result[i][j] = (long)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -68033,7 +68879,7 @@ public static unsafe long[][] DotWithTransposed(this long[][] a, double[,] b, lo #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] DotWithTransposed(this long[,] a, double[][] b, long[][] result) + public static long[][] DotWithTransposed(this long[,] a, double[][] b, long[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -68043,20 +68889,22 @@ public static unsafe long[][] DotWithTransposed(this long[,] a, double[][] b, lo #endif int n = a.Rows(); - fixed (long* A = a) - for (int j = 0; j < b.Length; j++) - { - long* pa = A; - for (int i = 0; i < n; i++) - { - long sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (long)((long)(*pa++) * (long)brow[k]); - result[i][j] = (long)sum; - } - } - + unsafe + { + fixed (long* A = a) + for (int j = 0; j < b.Length; j++) + { + long* pa = A; + for (int i = 0; i < n; i++) + { + long sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (long)((long)(*pa++) * (long)brow[k]); + result[i][j] = (long)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -68202,7 +69050,7 @@ public static long[][] DotWithTransposed(this long[][] a, double[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[,] TransposeAndDot(this long[,] a, double[,] b, long[,] result) + public static long[,] TransposeAndDot(this long[,] a, double[,] b, long[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -68213,35 +69061,37 @@ public static long[][] DotWithTransposed(this long[][] a, double[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (long* R = result) + fixed (double* B = b) + fixed (long* ptemp = new long[p]) + { + long* pr = R; - fixed (long* R = result) - fixed (double* B = b) - fixed (long* ptemp = new long[p]) - { - long* pr = R; - - for (int i = 0; i < m; i++) - { - long* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - long aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (long)((long)aval * (long)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (long)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + long* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + long aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (long)((long)aval * (long)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (long)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -68635,18 +69485,22 @@ public static long[][] DivideByDiagonal(this long[][] a, double[] diagonal, long #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[,] Outer(this long[] a, double[] b, long[,] result) + public static long[,] Outer(this long[] a, double[] b, long[,] result) { - fixed (long* R = result) - { - long* pr = R; - for (int i = 0; i < a.Length; i++) - { - long x = (long)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (long)(x * (long)b[j]); - } - } + unsafe + { + fixed (long* R = result) + { + long* pr = R; + for (int i = 0; i < a.Length; i++) + { + long x = (long)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (long)(x * (long)b[j]); + } + } + } + return result; } @@ -68809,25 +69663,28 @@ public static long[][] Kronecker(this long[][] a, double[][] b, long[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] Kronecker(this long[][] a, double[,] b, long[][] result) + public static long[][] Kronecker(this long[][] a, double[,] b, long[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - long aval = (long)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + long aval = (long)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (long)(aval * (long)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (long)(aval * (long)(*pb++)); + } + } + return result; } @@ -68881,18 +69738,22 @@ public static long[][] Kronecker(this long[,] a, double[][] b, long[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[] Kronecker(this long[] a, double[] b, long[] result) + public static long[] Kronecker(this long[] a, double[] b, long[] result) { - fixed (long* R = result) - { - long* pr = R; - for (int i = 0; i < a.Length; i++) - { - long x = (long)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (long)(x * (long)b[j]); - } - } + unsafe + { + fixed (long* R = result) + { + long* pr = R; + for (int i = 0; i < a.Length; i++) + { + long x = (long)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (long)(x * (long)b[j]); + } + } + } + return result; } @@ -69086,7 +69947,7 @@ public static double[][] Dot(this long[][] a, double[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this long[,] a, double[][] b, double[][] result) + public static double[][] Dot(this long[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -69100,21 +69961,24 @@ public static unsafe double[][] Dot(this long[,] a, double[][] b, double[][] res var t = new double[K]; - fixed (long* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (long* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - long* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + long* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -69192,7 +70056,7 @@ public static double[] Dot(this long[][] matrix, double[] columnVector, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this long[,] matrix, double[] columnVector, double[] result) + public static double[] Dot(this long[,] matrix, double[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -69201,55 +70065,54 @@ public static unsafe double[] Dot(this long[,] matrix, double[] columnVector, do if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (long* a = matrix) + fixed (double* x = columnVector) + fixed (double* r = result) + { + long* pa1 = a; + long* pa2 = a + cols; + double* pr = r; - fixed (long* a = matrix) - fixed (double* x = columnVector) - fixed (double* r = result) - { - long* pa1 = a; - long* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -69355,7 +70218,7 @@ public static double[] Dot(this long[] rowVector, double[][] matrix, double[] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this long[][] a, double[,] b, double[][] result) + public static double[][] DotWithTransposed(this long[][] a, double[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -69365,19 +70228,22 @@ public static unsafe double[][] DotWithTransposed(this long[][] a, double[,] b, #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - long[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + long[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -69399,7 +70265,7 @@ public static unsafe double[][] DotWithTransposed(this long[][] a, double[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this long[,] a, double[][] b, double[][] result) + public static double[][] DotWithTransposed(this long[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -69409,20 +70275,22 @@ public static unsafe double[][] DotWithTransposed(this long[,] a, double[][] b, #endif int n = a.Rows(); - fixed (long* A = a) - for (int j = 0; j < b.Length; j++) - { - long* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (long* A = a) + for (int j = 0; j < b.Length; j++) + { + long* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -69568,7 +70436,7 @@ public static double[][] DotWithTransposed(this long[][] a, double[] columnVecto #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this long[,] a, double[,] b, double[,] result) + public static double[,] TransposeAndDot(this long[,] a, double[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -69579,35 +70447,37 @@ public static double[][] DotWithTransposed(this long[][] a, double[] columnVecto throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (double* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (double* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - long aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + long aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -70001,18 +70871,22 @@ public static double[][] DivideByDiagonal(this long[][] a, double[] diagonal, do #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this long[] a, double[] b, double[,] result) + public static double[,] Outer(this long[] a, double[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -70175,25 +71049,28 @@ public static double[][] Kronecker(this long[][] a, double[][] b, double[][] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this long[][] a, double[,] b, double[][] result) + public static double[][] Kronecker(this long[][] a, double[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -70247,18 +71124,22 @@ public static double[][] Kronecker(this long[,] a, double[][] b, double[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this long[] a, double[] b, double[] result) + public static double[] Kronecker(this long[] a, double[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -70452,7 +71333,7 @@ public static int[][] Dot(this long[][] a, double[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this long[,] a, double[][] b, int[][] result) + public static int[][] Dot(this long[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -70466,21 +71347,24 @@ public static unsafe int[][] Dot(this long[,] a, double[][] b, int[][] result) var t = new double[K]; - fixed (long* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (long* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - long* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + long* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -70558,7 +71442,7 @@ public static int[] Dot(this long[][] matrix, double[] columnVector, int[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this long[,] matrix, double[] columnVector, int[] result) + public static int[] Dot(this long[,] matrix, double[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -70567,55 +71451,54 @@ public static unsafe int[] Dot(this long[,] matrix, double[] columnVector, int[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (long* a = matrix) + fixed (double* x = columnVector) + fixed (int* r = result) + { + long* pa1 = a; + long* pa2 = a + cols; + int* pr = r; - fixed (long* a = matrix) - fixed (double* x = columnVector) - fixed (int* r = result) - { - long* pa1 = a; - long* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -70721,7 +71604,7 @@ public static int[] Dot(this long[] rowVector, double[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this long[][] a, double[,] b, int[][] result) + public static int[][] DotWithTransposed(this long[][] a, double[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -70731,19 +71614,22 @@ public static unsafe int[][] DotWithTransposed(this long[][] a, double[,] b, int #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - long[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + long[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -70765,7 +71651,7 @@ public static unsafe int[][] DotWithTransposed(this long[][] a, double[,] b, int #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this long[,] a, double[][] b, int[][] result) + public static int[][] DotWithTransposed(this long[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -70775,20 +71661,22 @@ public static unsafe int[][] DotWithTransposed(this long[,] a, double[][] b, int #endif int n = a.Rows(); - fixed (long* A = a) - for (int j = 0; j < b.Length; j++) - { - long* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (long* A = a) + for (int j = 0; j < b.Length; j++) + { + long* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -70934,7 +71822,7 @@ public static int[][] DotWithTransposed(this long[][] a, double[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this long[,] a, double[,] b, int[,] result) + public static int[,] TransposeAndDot(this long[,] a, double[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -70945,35 +71833,37 @@ public static int[][] DotWithTransposed(this long[][] a, double[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (double* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (double* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - long aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + long aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -71367,18 +72257,22 @@ public static int[][] DivideByDiagonal(this long[][] a, double[] diagonal, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this long[] a, double[] b, int[,] result) + public static int[,] Outer(this long[] a, double[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -71541,25 +72435,28 @@ public static int[][] Kronecker(this long[][] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this long[][] a, double[,] b, int[][] result) + public static int[][] Kronecker(this long[][] a, double[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -71613,18 +72510,22 @@ public static int[][] Kronecker(this long[,] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this long[] a, double[] b, int[] result) + public static int[] Kronecker(this long[] a, double[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -71818,7 +72719,7 @@ public static long[][] Dot(this long[][] a, int[,] b, long[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] Dot(this long[,] a, int[][] b, long[][] result) + public static long[][] Dot(this long[,] a, int[][] b, long[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -71832,21 +72733,24 @@ public static unsafe long[][] Dot(this long[,] a, int[][] b, long[][] result) var t = new int[K]; - fixed (long* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (long* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - long* pa = A; - for (int i = 0; i < N; i++) - { - long s = (long)0; - for (int k = 0; k < t.Length; k++) - s += (long)((long)(*pa++) * (long)t[k]); - result[i][j] = (long)s; - } - } + long* pa = A; + for (int i = 0; i < N; i++) + { + long s = (long)0; + for (int k = 0; k < t.Length; k++) + s += (long)((long)(*pa++) * (long)t[k]); + result[i][j] = (long)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -71924,7 +72828,7 @@ public static long[] Dot(this long[][] matrix, int[] columnVector, long[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[] Dot(this long[,] matrix, int[] columnVector, long[] result) + public static long[] Dot(this long[,] matrix, int[] columnVector, long[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -71933,55 +72837,54 @@ public static unsafe long[] Dot(this long[,] matrix, int[] columnVector, long[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (long* a = matrix) + fixed (int* x = columnVector) + fixed (long* r = result) + { + long* pa1 = a; + long* pa2 = a + cols; + long* pr = r; - fixed (long* a = matrix) - fixed (int* x = columnVector) - fixed (long* r = result) - { - long* pa1 = a; - long* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - long sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (long)((long)(*pa1++) * (long)(*px)); - sum2 += (long)((long)(*pa2++) * (long)(*px)); - px++; - } - - *pr++ = (long)sum1; - *pr++ = (long)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + long sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (long)((long)(*pa1++) * (long)(*px)); + sum2 += (long)((long)(*pa2++) * (long)(*px)); + px++; + } + + *pr++ = (long)sum1; + *pr++ = (long)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - long sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + long sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (long)((long)(*pa1++) * (long)(*px++)); + for (int j = 0; j < cols; j++) + sum += (long)((long)(*pa1++) * (long)(*px++)); - *pr = (long)sum; - } - } + *pr = (long)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -72087,7 +72990,7 @@ public static long[] Dot(this long[] rowVector, int[][] matrix, long[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] DotWithTransposed(this long[][] a, int[,] b, long[][] result) + public static long[][] DotWithTransposed(this long[][] a, int[,] b, long[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -72097,19 +73000,22 @@ public static unsafe long[][] DotWithTransposed(this long[][] a, int[,] b, long[ #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - long[] arow = a[i]; - for (int j = 0; j < n; j++) - { - long sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (long)((long)arow[k] * (long)(*pb++)); - result[i][j] = (long)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + long[] arow = a[i]; + for (int j = 0; j < n; j++) + { + long sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (long)((long)arow[k] * (long)(*pb++)); + result[i][j] = (long)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -72131,7 +73037,7 @@ public static unsafe long[][] DotWithTransposed(this long[][] a, int[,] b, long[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] DotWithTransposed(this long[,] a, int[][] b, long[][] result) + public static long[][] DotWithTransposed(this long[,] a, int[][] b, long[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -72141,20 +73047,22 @@ public static unsafe long[][] DotWithTransposed(this long[,] a, int[][] b, long[ #endif int n = a.Rows(); - fixed (long* A = a) - for (int j = 0; j < b.Length; j++) - { - long* pa = A; - for (int i = 0; i < n; i++) - { - long sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (long)((long)(*pa++) * (long)brow[k]); - result[i][j] = (long)sum; - } - } - + unsafe + { + fixed (long* A = a) + for (int j = 0; j < b.Length; j++) + { + long* pa = A; + for (int i = 0; i < n; i++) + { + long sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (long)((long)(*pa++) * (long)brow[k]); + result[i][j] = (long)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -72300,7 +73208,7 @@ public static long[][] DotWithTransposed(this long[][] a, int[] columnVector, lo #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[,] TransposeAndDot(this long[,] a, int[,] b, long[,] result) + public static long[,] TransposeAndDot(this long[,] a, int[,] b, long[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -72311,35 +73219,37 @@ public static long[][] DotWithTransposed(this long[][] a, int[] columnVector, lo throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (long* R = result) + fixed (int* B = b) + fixed (long* ptemp = new long[p]) + { + long* pr = R; - fixed (long* R = result) - fixed (int* B = b) - fixed (long* ptemp = new long[p]) - { - long* pr = R; - - for (int i = 0; i < m; i++) - { - long* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - long aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (long)((long)aval * (long)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (long)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + long* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + long aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (long)((long)aval * (long)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (long)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -72733,18 +73643,22 @@ public static long[][] DivideByDiagonal(this long[][] a, int[] diagonal, long[][ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[,] Outer(this long[] a, int[] b, long[,] result) + public static long[,] Outer(this long[] a, int[] b, long[,] result) { - fixed (long* R = result) - { - long* pr = R; - for (int i = 0; i < a.Length; i++) - { - long x = (long)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (long)(x * (long)b[j]); - } - } + unsafe + { + fixed (long* R = result) + { + long* pr = R; + for (int i = 0; i < a.Length; i++) + { + long x = (long)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (long)(x * (long)b[j]); + } + } + } + return result; } @@ -72907,25 +73821,28 @@ public static long[][] Kronecker(this long[][] a, int[][] b, long[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] Kronecker(this long[][] a, int[,] b, long[][] result) + public static long[][] Kronecker(this long[][] a, int[,] b, long[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - long aval = (long)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + long aval = (long)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (long)(aval * (long)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (long)(aval * (long)(*pb++)); + } + } + return result; } @@ -72979,18 +73896,22 @@ public static long[][] Kronecker(this long[,] a, int[][] b, long[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[] Kronecker(this long[] a, int[] b, long[] result) + public static long[] Kronecker(this long[] a, int[] b, long[] result) { - fixed (long* R = result) - { - long* pr = R; - for (int i = 0; i < a.Length; i++) - { - long x = (long)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (long)(x * (long)b[j]); - } - } + unsafe + { + fixed (long* R = result) + { + long* pr = R; + for (int i = 0; i < a.Length; i++) + { + long x = (long)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (long)(x * (long)b[j]); + } + } + } + return result; } @@ -73184,7 +74105,7 @@ public static int[][] Dot(this long[][] a, int[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this long[,] a, int[][] b, int[][] result) + public static int[][] Dot(this long[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -73198,21 +74119,24 @@ public static unsafe int[][] Dot(this long[,] a, int[][] b, int[][] result) var t = new int[K]; - fixed (long* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (long* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - long* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + long* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -73290,7 +74214,7 @@ public static int[] Dot(this long[][] matrix, int[] columnVector, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this long[,] matrix, int[] columnVector, int[] result) + public static int[] Dot(this long[,] matrix, int[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -73299,55 +74223,54 @@ public static unsafe int[] Dot(this long[,] matrix, int[] columnVector, int[] re if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (long* a = matrix) + fixed (int* x = columnVector) + fixed (int* r = result) + { + long* pa1 = a; + long* pa2 = a + cols; + int* pr = r; - fixed (long* a = matrix) - fixed (int* x = columnVector) - fixed (int* r = result) - { - long* pa1 = a; - long* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -73453,7 +74376,7 @@ public static int[] Dot(this long[] rowVector, int[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this long[][] a, int[,] b, int[][] result) + public static int[][] DotWithTransposed(this long[][] a, int[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -73463,19 +74386,22 @@ public static unsafe int[][] DotWithTransposed(this long[][] a, int[,] b, int[][ #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - long[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + long[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -73497,7 +74423,7 @@ public static unsafe int[][] DotWithTransposed(this long[][] a, int[,] b, int[][ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this long[,] a, int[][] b, int[][] result) + public static int[][] DotWithTransposed(this long[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -73507,20 +74433,22 @@ public static unsafe int[][] DotWithTransposed(this long[,] a, int[][] b, int[][ #endif int n = a.Rows(); - fixed (long* A = a) - for (int j = 0; j < b.Length; j++) - { - long* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (long* A = a) + for (int j = 0; j < b.Length; j++) + { + long* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -73666,7 +74594,7 @@ public static int[][] DotWithTransposed(this long[][] a, int[] columnVector, int #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this long[,] a, int[,] b, int[,] result) + public static int[,] TransposeAndDot(this long[,] a, int[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -73677,35 +74605,37 @@ public static int[][] DotWithTransposed(this long[][] a, int[] columnVector, int throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (int* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (int* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - long aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + long aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -74099,18 +75029,22 @@ public static int[][] DivideByDiagonal(this long[][] a, int[] diagonal, int[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this long[] a, int[] b, int[,] result) + public static int[,] Outer(this long[] a, int[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -74273,25 +75207,28 @@ public static int[][] Kronecker(this long[][] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this long[][] a, int[,] b, int[][] result) + public static int[][] Kronecker(this long[][] a, int[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -74345,18 +75282,22 @@ public static int[][] Kronecker(this long[,] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this long[] a, int[] b, int[] result) + public static int[] Kronecker(this long[] a, int[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -74550,7 +75491,7 @@ public static double[][] Dot(this long[][] a, int[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this long[,] a, int[][] b, double[][] result) + public static double[][] Dot(this long[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -74564,21 +75505,24 @@ public static unsafe double[][] Dot(this long[,] a, int[][] b, double[][] result var t = new int[K]; - fixed (long* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (long* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - long* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + long* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -74656,7 +75600,7 @@ public static double[] Dot(this long[][] matrix, int[] columnVector, double[] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this long[,] matrix, int[] columnVector, double[] result) + public static double[] Dot(this long[,] matrix, int[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -74665,55 +75609,54 @@ public static unsafe double[] Dot(this long[,] matrix, int[] columnVector, doubl if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (long* a = matrix) + fixed (int* x = columnVector) + fixed (double* r = result) + { + long* pa1 = a; + long* pa2 = a + cols; + double* pr = r; - fixed (long* a = matrix) - fixed (int* x = columnVector) - fixed (double* r = result) - { - long* pa1 = a; - long* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -74819,7 +75762,7 @@ public static double[] Dot(this long[] rowVector, int[][] matrix, double[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this long[][] a, int[,] b, double[][] result) + public static double[][] DotWithTransposed(this long[][] a, int[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -74829,19 +75772,22 @@ public static unsafe double[][] DotWithTransposed(this long[][] a, int[,] b, dou #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - long[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + long[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -74863,7 +75809,7 @@ public static unsafe double[][] DotWithTransposed(this long[][] a, int[,] b, dou #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this long[,] a, int[][] b, double[][] result) + public static double[][] DotWithTransposed(this long[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -74873,20 +75819,22 @@ public static unsafe double[][] DotWithTransposed(this long[,] a, int[][] b, dou #endif int n = a.Rows(); - fixed (long* A = a) - for (int j = 0; j < b.Length; j++) - { - long* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (long* A = a) + for (int j = 0; j < b.Length; j++) + { + long* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -75032,7 +75980,7 @@ public static double[][] DotWithTransposed(this long[][] a, int[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this long[,] a, int[,] b, double[,] result) + public static double[,] TransposeAndDot(this long[,] a, int[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -75043,35 +75991,37 @@ public static double[][] DotWithTransposed(this long[][] a, int[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (int* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (int* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - long aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + long aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -75465,18 +76415,22 @@ public static double[][] DivideByDiagonal(this long[][] a, int[] diagonal, doubl #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this long[] a, int[] b, double[,] result) + public static double[,] Outer(this long[] a, int[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -75639,25 +76593,28 @@ public static double[][] Kronecker(this long[][] a, int[][] b, double[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this long[][] a, int[,] b, double[][] result) + public static double[][] Kronecker(this long[][] a, int[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -75711,18 +76668,22 @@ public static double[][] Kronecker(this long[,] a, int[][] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this long[] a, int[] b, double[] result) + public static double[] Kronecker(this long[] a, int[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -75916,7 +76877,7 @@ public static long[][] Dot(this long[][] a, float[,] b, long[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] Dot(this long[,] a, float[][] b, long[][] result) + public static long[][] Dot(this long[,] a, float[][] b, long[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -75930,21 +76891,24 @@ public static unsafe long[][] Dot(this long[,] a, float[][] b, long[][] result) var t = new float[K]; - fixed (long* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (long* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - long* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (long)s; - } - } + long* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (long)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -76022,7 +76986,7 @@ public static long[] Dot(this long[][] matrix, float[] columnVector, long[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[] Dot(this long[,] matrix, float[] columnVector, long[] result) + public static long[] Dot(this long[,] matrix, float[] columnVector, long[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -76031,55 +76995,54 @@ public static unsafe long[] Dot(this long[,] matrix, float[] columnVector, long[ if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (long* a = matrix) + fixed (float* x = columnVector) + fixed (long* r = result) + { + long* pa1 = a; + long* pa2 = a + cols; + long* pr = r; - fixed (long* a = matrix) - fixed (float* x = columnVector) - fixed (long* r = result) - { - long* pa1 = a; - long* pa2 = a + cols; - long* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (long)sum1; - *pr++ = (long)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (long)sum1; + *pr++ = (long)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (long)sum; - } - } + *pr = (long)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -76185,7 +77148,7 @@ public static long[] Dot(this long[] rowVector, float[][] matrix, long[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] DotWithTransposed(this long[][] a, float[,] b, long[][] result) + public static long[][] DotWithTransposed(this long[][] a, float[,] b, long[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -76195,19 +77158,22 @@ public static unsafe long[][] DotWithTransposed(this long[][] a, float[,] b, lon #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - long[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (long)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + long[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (long)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -76229,7 +77195,7 @@ public static unsafe long[][] DotWithTransposed(this long[][] a, float[,] b, lon #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] DotWithTransposed(this long[,] a, float[][] b, long[][] result) + public static long[][] DotWithTransposed(this long[,] a, float[][] b, long[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -76239,20 +77205,22 @@ public static unsafe long[][] DotWithTransposed(this long[,] a, float[][] b, lon #endif int n = a.Rows(); - fixed (long* A = a) - for (int j = 0; j < b.Length; j++) - { - long* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (long)sum; - } - } - + unsafe + { + fixed (long* A = a) + for (int j = 0; j < b.Length; j++) + { + long* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (long)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -76398,7 +77366,7 @@ public static long[][] DotWithTransposed(this long[][] a, float[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[,] TransposeAndDot(this long[,] a, float[,] b, long[,] result) + public static long[,] TransposeAndDot(this long[,] a, float[,] b, long[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -76409,35 +77377,37 @@ public static long[][] DotWithTransposed(this long[][] a, float[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (long* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + long* pr = R; - fixed (long* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - long* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - long aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (long)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + long aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (long)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -76831,18 +77801,22 @@ public static long[][] DivideByDiagonal(this long[][] a, float[] diagonal, long[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[,] Outer(this long[] a, float[] b, long[,] result) + public static long[,] Outer(this long[] a, float[] b, long[,] result) { - fixed (long* R = result) - { - long* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (long)(x * (double)b[j]); - } - } + unsafe + { + fixed (long* R = result) + { + long* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (long)(x * (double)b[j]); + } + } + } + return result; } @@ -77005,25 +77979,28 @@ public static long[][] Kronecker(this long[][] a, float[][] b, long[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[][] Kronecker(this long[][] a, float[,] b, long[][] result) + public static long[][] Kronecker(this long[][] a, float[,] b, long[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (long)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (long)(aval * (double)(*pb++)); + } + } + return result; } @@ -77077,18 +78054,22 @@ public static long[][] Kronecker(this long[,] a, float[][] b, long[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe long[] Kronecker(this long[] a, float[] b, long[] result) + public static long[] Kronecker(this long[] a, float[] b, long[] result) { - fixed (long* R = result) - { - long* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (long)(x * (double)b[j]); - } - } + unsafe + { + fixed (long* R = result) + { + long* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (long)(x * (double)b[j]); + } + } + } + return result; } @@ -77282,7 +78263,7 @@ public static float[][] Dot(this long[][] a, float[,] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Dot(this long[,] a, float[][] b, float[][] result) + public static float[][] Dot(this long[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -77296,21 +78277,24 @@ public static unsafe float[][] Dot(this long[,] a, float[][] b, float[][] result var t = new float[K]; - fixed (long* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (long* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - long* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (float)s; - } - } + long* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (float)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -77388,7 +78372,7 @@ public static float[] Dot(this long[][] matrix, float[] columnVector, float[] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Dot(this long[,] matrix, float[] columnVector, float[] result) + public static float[] Dot(this long[,] matrix, float[] columnVector, float[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -77397,55 +78381,54 @@ public static unsafe float[] Dot(this long[,] matrix, float[] columnVector, floa if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (long* a = matrix) + fixed (float* x = columnVector) + fixed (float* r = result) + { + long* pa1 = a; + long* pa2 = a + cols; + float* pr = r; - fixed (long* a = matrix) - fixed (float* x = columnVector) - fixed (float* r = result) - { - long* pa1 = a; - long* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (float)sum1; - *pr++ = (float)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (float)sum1; + *pr++ = (float)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (float)sum; - } - } + *pr = (float)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -77551,7 +78534,7 @@ public static float[] Dot(this long[] rowVector, float[][] matrix, float[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this long[][] a, float[,] b, float[][] result) + public static float[][] DotWithTransposed(this long[][] a, float[,] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -77561,19 +78544,22 @@ public static unsafe float[][] DotWithTransposed(this long[][] a, float[,] b, fl #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - long[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (float)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + long[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -77595,7 +78581,7 @@ public static unsafe float[][] DotWithTransposed(this long[][] a, float[,] b, fl #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this long[,] a, float[][] b, float[][] result) + public static float[][] DotWithTransposed(this long[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -77605,20 +78591,22 @@ public static unsafe float[][] DotWithTransposed(this long[,] a, float[][] b, fl #endif int n = a.Rows(); - fixed (long* A = a) - for (int j = 0; j < b.Length; j++) - { - long* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (float)sum; - } - } - + unsafe + { + fixed (long* A = a) + for (int j = 0; j < b.Length; j++) + { + long* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -77764,7 +78752,7 @@ public static float[][] DotWithTransposed(this long[][] a, float[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] TransposeAndDot(this long[,] a, float[,] b, float[,] result) + public static float[,] TransposeAndDot(this long[,] a, float[,] b, float[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -77775,35 +78763,37 @@ public static float[][] DotWithTransposed(this long[][] a, float[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (float* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + float* pr = R; - fixed (float* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - float* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - long aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (float)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + long aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (float)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -78197,18 +79187,22 @@ public static float[][] DivideByDiagonal(this long[][] a, float[] diagonal, floa #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] Outer(this long[] a, float[] b, float[,] result) + public static float[,] Outer(this long[] a, float[] b, float[,] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -78371,25 +79365,28 @@ public static float[][] Kronecker(this long[][] a, float[][] b, float[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Kronecker(this long[][] a, float[,] b, float[][] result) + public static float[][] Kronecker(this long[][] a, float[,] b, float[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); + } + } + return result; } @@ -78443,18 +79440,22 @@ public static float[][] Kronecker(this long[,] a, float[][] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Kronecker(this long[] a, float[] b, float[] result) + public static float[] Kronecker(this long[] a, float[] b, float[] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -78648,7 +79649,7 @@ public static double[][] Dot(this long[][] a, float[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this long[,] a, float[][] b, double[][] result) + public static double[][] Dot(this long[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -78662,21 +79663,24 @@ public static unsafe double[][] Dot(this long[,] a, float[][] b, double[][] resu var t = new float[K]; - fixed (long* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (long* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - long* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + long* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -78754,7 +79758,7 @@ public static double[] Dot(this long[][] matrix, float[] columnVector, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this long[,] matrix, float[] columnVector, double[] result) + public static double[] Dot(this long[,] matrix, float[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -78763,55 +79767,54 @@ public static unsafe double[] Dot(this long[,] matrix, float[] columnVector, dou if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (long* a = matrix) + fixed (float* x = columnVector) + fixed (double* r = result) + { + long* pa1 = a; + long* pa2 = a + cols; + double* pr = r; - fixed (long* a = matrix) - fixed (float* x = columnVector) - fixed (double* r = result) - { - long* pa1 = a; - long* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -78917,7 +79920,7 @@ public static double[] Dot(this long[] rowVector, float[][] matrix, double[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this long[][] a, float[,] b, double[][] result) + public static double[][] DotWithTransposed(this long[][] a, float[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -78927,19 +79930,22 @@ public static unsafe double[][] DotWithTransposed(this long[][] a, float[,] b, d #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - long[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + long[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -78961,7 +79967,7 @@ public static unsafe double[][] DotWithTransposed(this long[][] a, float[,] b, d #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this long[,] a, float[][] b, double[][] result) + public static double[][] DotWithTransposed(this long[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -78971,20 +79977,22 @@ public static unsafe double[][] DotWithTransposed(this long[,] a, float[][] b, d #endif int n = a.Rows(); - fixed (long* A = a) - for (int j = 0; j < b.Length; j++) - { - long* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (long* A = a) + for (int j = 0; j < b.Length; j++) + { + long* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -79130,7 +80138,7 @@ public static double[][] DotWithTransposed(this long[][] a, float[] columnVector #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this long[,] a, float[,] b, double[,] result) + public static double[,] TransposeAndDot(this long[,] a, float[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -79141,35 +80149,37 @@ public static double[][] DotWithTransposed(this long[][] a, float[] columnVector throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - long aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + long aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -79563,18 +80573,22 @@ public static double[][] DivideByDiagonal(this long[][] a, float[] diagonal, dou #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this long[] a, float[] b, double[,] result) + public static double[,] Outer(this long[] a, float[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -79737,25 +80751,28 @@ public static double[][] Kronecker(this long[][] a, float[][] b, double[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this long[][] a, float[,] b, double[][] result) + public static double[][] Kronecker(this long[][] a, float[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -79809,18 +80826,22 @@ public static double[][] Kronecker(this long[,] a, float[][] b, double[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this long[] a, float[] b, double[] result) + public static double[] Kronecker(this long[] a, float[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -80014,7 +81035,7 @@ public static int[][] Dot(this long[][] a, float[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this long[,] a, float[][] b, int[][] result) + public static int[][] Dot(this long[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -80028,21 +81049,24 @@ public static unsafe int[][] Dot(this long[,] a, float[][] b, int[][] result) var t = new float[K]; - fixed (long* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (long* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - long* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (int)s; - } - } + long* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -80120,7 +81144,7 @@ public static int[] Dot(this long[][] matrix, float[] columnVector, int[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this long[,] matrix, float[] columnVector, int[] result) + public static int[] Dot(this long[,] matrix, float[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -80129,55 +81153,54 @@ public static unsafe int[] Dot(this long[,] matrix, float[] columnVector, int[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (long* a = matrix) + fixed (float* x = columnVector) + fixed (int* r = result) + { + long* pa1 = a; + long* pa2 = a + cols; + int* pr = r; - fixed (long* a = matrix) - fixed (float* x = columnVector) - fixed (int* r = result) - { - long* pa1 = a; - long* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -80283,7 +81306,7 @@ public static int[] Dot(this long[] rowVector, float[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this long[][] a, float[,] b, int[][] result) + public static int[][] DotWithTransposed(this long[][] a, float[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -80293,19 +81316,22 @@ public static unsafe int[][] DotWithTransposed(this long[][] a, float[,] b, int[ #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - long[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + long[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -80327,7 +81353,7 @@ public static unsafe int[][] DotWithTransposed(this long[][] a, float[,] b, int[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this long[,] a, float[][] b, int[][] result) + public static int[][] DotWithTransposed(this long[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -80337,20 +81363,22 @@ public static unsafe int[][] DotWithTransposed(this long[,] a, float[][] b, int[ #endif int n = a.Rows(); - fixed (long* A = a) - for (int j = 0; j < b.Length; j++) - { - long* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (long* A = a) + for (int j = 0; j < b.Length; j++) + { + long* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -80496,7 +81524,7 @@ public static int[][] DotWithTransposed(this long[][] a, float[] columnVector, i #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this long[,] a, float[,] b, int[,] result) + public static int[,] TransposeAndDot(this long[,] a, float[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -80507,35 +81535,37 @@ public static int[][] DotWithTransposed(this long[][] a, float[] columnVector, i throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - long aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + long aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -80929,18 +81959,22 @@ public static int[][] DivideByDiagonal(this long[][] a, float[] diagonal, int[][ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this long[] a, float[] b, int[,] result) + public static int[,] Outer(this long[] a, float[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -81103,25 +82137,28 @@ public static int[][] Kronecker(this long[][] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this long[][] a, float[,] b, int[][] result) + public static int[][] Kronecker(this long[][] a, float[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); + } + } + return result; } @@ -81175,18 +82212,22 @@ public static int[][] Kronecker(this long[,] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this long[] a, float[] b, int[] result) + public static int[] Kronecker(this long[] a, float[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -81380,7 +82421,7 @@ public static decimal[][] Dot(this decimal[][] a, decimal[,] b, decimal[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] Dot(this decimal[,] a, decimal[][] b, decimal[][] result) + public static decimal[][] Dot(this decimal[,] a, decimal[][] b, decimal[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -81394,21 +82435,24 @@ public static unsafe decimal[][] Dot(this decimal[,] a, decimal[][] b, decimal[] var t = new decimal[K]; - fixed (decimal* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - decimal* pa = A; - for (int i = 0; i < N; i++) - { - decimal s = (decimal)0; - for (int k = 0; k < t.Length; k++) - s += (decimal)((decimal)(*pa++) * (decimal)t[k]); - result[i][j] = (decimal)s; - } - } + decimal* pa = A; + for (int i = 0; i < N; i++) + { + decimal s = (decimal)0; + for (int k = 0; k < t.Length; k++) + s += (decimal)((decimal)(*pa++) * (decimal)t[k]); + result[i][j] = (decimal)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -81486,7 +82530,7 @@ public static decimal[] Dot(this decimal[][] matrix, decimal[] columnVector, dec #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[] Dot(this decimal[,] matrix, decimal[] columnVector, decimal[] result) + public static decimal[] Dot(this decimal[,] matrix, decimal[] columnVector, decimal[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -81495,55 +82539,54 @@ public static unsafe decimal[] Dot(this decimal[,] matrix, decimal[] columnVecto if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (decimal* a = matrix) + fixed (decimal* x = columnVector) + fixed (decimal* r = result) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + decimal* pr = r; - fixed (decimal* a = matrix) - fixed (decimal* x = columnVector) - fixed (decimal* r = result) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - decimal sum1 = 0, sum2 = 0; - decimal* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (decimal)((decimal)(*pa1++) * (decimal)(*px)); - sum2 += (decimal)((decimal)(*pa2++) * (decimal)(*px)); - px++; - } - - *pr++ = (decimal)sum1; - *pr++ = (decimal)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + decimal sum1 = 0, sum2 = 0; + decimal* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (decimal)((decimal)(*pa1++) * (decimal)(*px)); + sum2 += (decimal)((decimal)(*pa2++) * (decimal)(*px)); + px++; + } + + *pr++ = (decimal)sum1; + *pr++ = (decimal)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - decimal sum = 0; - decimal* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + decimal sum = 0; + decimal* px = x; - for (int j = 0; j < cols; j++) - sum += (decimal)((decimal)(*pa1++) * (decimal)(*px++)); + for (int j = 0; j < cols; j++) + sum += (decimal)((decimal)(*pa1++) * (decimal)(*px++)); - *pr = (decimal)sum; - } - } + *pr = (decimal)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -81649,7 +82692,7 @@ public static decimal[] Dot(this decimal[] rowVector, decimal[][] matrix, decima #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] DotWithTransposed(this decimal[][] a, decimal[,] b, decimal[][] result) + public static decimal[][] DotWithTransposed(this decimal[][] a, decimal[,] b, decimal[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -81659,19 +82702,22 @@ public static unsafe decimal[][] DotWithTransposed(this decimal[][] a, decimal[, #endif int n = b.Rows(); - fixed (decimal* B = b) - for (int i = 0; i < a.Length; i++) - { - decimal* pb = B; - decimal[] arow = a[i]; - for (int j = 0; j < n; j++) - { - decimal sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (decimal)((decimal)arow[k] * (decimal)(*pb++)); - result[i][j] = (decimal)sum; - } - } + unsafe + { + fixed (decimal* B = b) + for (int i = 0; i < a.Length; i++) + { + decimal* pb = B; + decimal[] arow = a[i]; + for (int j = 0; j < n; j++) + { + decimal sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (decimal)((decimal)arow[k] * (decimal)(*pb++)); + result[i][j] = (decimal)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -81693,7 +82739,7 @@ public static unsafe decimal[][] DotWithTransposed(this decimal[][] a, decimal[, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] DotWithTransposed(this decimal[,] a, decimal[][] b, decimal[][] result) + public static decimal[][] DotWithTransposed(this decimal[,] a, decimal[][] b, decimal[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -81703,20 +82749,22 @@ public static unsafe decimal[][] DotWithTransposed(this decimal[,] a, decimal[][ #endif int n = a.Rows(); - fixed (decimal* A = a) - for (int j = 0; j < b.Length; j++) - { - decimal* pa = A; - for (int i = 0; i < n; i++) - { - decimal sum = 0; - decimal[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (decimal)((decimal)(*pa++) * (decimal)brow[k]); - result[i][j] = (decimal)sum; - } - } - + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < b.Length; j++) + { + decimal* pa = A; + for (int i = 0; i < n; i++) + { + decimal sum = 0; + decimal[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (decimal)((decimal)(*pa++) * (decimal)brow[k]); + result[i][j] = (decimal)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -81862,7 +82910,7 @@ public static decimal[][] DotWithTransposed(this decimal[][] a, decimal[] column #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[,] TransposeAndDot(this decimal[,] a, decimal[,] b, decimal[,] result) + public static decimal[,] TransposeAndDot(this decimal[,] a, decimal[,] b, decimal[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -81873,35 +82921,37 @@ public static decimal[][] DotWithTransposed(this decimal[][] a, decimal[] column throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (decimal* R = result) + fixed (decimal* B = b) + fixed (decimal* ptemp = new decimal[p]) + { + decimal* pr = R; - fixed (decimal* R = result) - fixed (decimal* B = b) - fixed (decimal* ptemp = new decimal[p]) - { - decimal* pr = R; - - for (int i = 0; i < m; i++) - { - decimal* pt = ptemp; - decimal* pb = B; - - for (int k = 0; k < n; k++) - { - decimal aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (decimal)((decimal)aval * (decimal)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (decimal)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + decimal* pt = ptemp; + decimal* pb = B; + + for (int k = 0; k < n; k++) + { + decimal aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (decimal)((decimal)aval * (decimal)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (decimal)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -82295,18 +83345,22 @@ public static decimal[][] DivideByDiagonal(this decimal[][] a, decimal[] diagona #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[,] Outer(this decimal[] a, decimal[] b, decimal[,] result) + public static decimal[,] Outer(this decimal[] a, decimal[] b, decimal[,] result) { - fixed (decimal* R = result) - { - decimal* pr = R; - for (int i = 0; i < a.Length; i++) - { - decimal x = (decimal)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (decimal)(x * (decimal)b[j]); - } - } + unsafe + { + fixed (decimal* R = result) + { + decimal* pr = R; + for (int i = 0; i < a.Length; i++) + { + decimal x = (decimal)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (decimal)(x * (decimal)b[j]); + } + } + } + return result; } @@ -82469,25 +83523,28 @@ public static decimal[][] Kronecker(this decimal[][] a, decimal[][] b, decimal[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] Kronecker(this decimal[][] a, decimal[,] b, decimal[][] result) + public static decimal[][] Kronecker(this decimal[][] a, decimal[,] b, decimal[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (decimal* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - decimal aval = (decimal)a[i][j]; - decimal* pb = B; + unsafe + { + fixed (decimal* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + decimal aval = (decimal)a[i][j]; + decimal* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (decimal)(aval * (decimal)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (decimal)(aval * (decimal)(*pb++)); + } + } + return result; } @@ -82541,18 +83598,22 @@ public static decimal[][] Kronecker(this decimal[,] a, decimal[][] b, decimal[][ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[] Kronecker(this decimal[] a, decimal[] b, decimal[] result) + public static decimal[] Kronecker(this decimal[] a, decimal[] b, decimal[] result) { - fixed (decimal* R = result) - { - decimal* pr = R; - for (int i = 0; i < a.Length; i++) - { - decimal x = (decimal)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (decimal)(x * (decimal)b[j]); - } - } + unsafe + { + fixed (decimal* R = result) + { + decimal* pr = R; + for (int i = 0; i < a.Length; i++) + { + decimal x = (decimal)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (decimal)(x * (decimal)b[j]); + } + } + } + return result; } @@ -82746,7 +83807,7 @@ public static double[][] Dot(this decimal[][] a, decimal[,] b, double[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this decimal[,] a, decimal[][] b, double[][] result) + public static double[][] Dot(this decimal[,] a, decimal[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -82760,21 +83821,24 @@ public static unsafe double[][] Dot(this decimal[,] a, decimal[][] b, double[][] var t = new decimal[K]; - fixed (decimal* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - decimal* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + decimal* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -82852,7 +83916,7 @@ public static double[] Dot(this decimal[][] matrix, decimal[] columnVector, doub #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this decimal[,] matrix, decimal[] columnVector, double[] result) + public static double[] Dot(this decimal[,] matrix, decimal[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -82861,55 +83925,54 @@ public static unsafe double[] Dot(this decimal[,] matrix, decimal[] columnVector if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (decimal* a = matrix) + fixed (decimal* x = columnVector) + fixed (double* r = result) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + double* pr = r; - fixed (decimal* a = matrix) - fixed (decimal* x = columnVector) - fixed (double* r = result) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - decimal* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + decimal* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - decimal* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + decimal* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -83015,7 +84078,7 @@ public static double[] Dot(this decimal[] rowVector, decimal[][] matrix, double[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this decimal[][] a, decimal[,] b, double[][] result) + public static double[][] DotWithTransposed(this decimal[][] a, decimal[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -83025,19 +84088,22 @@ public static unsafe double[][] DotWithTransposed(this decimal[][] a, decimal[,] #endif int n = b.Rows(); - fixed (decimal* B = b) - for (int i = 0; i < a.Length; i++) - { - decimal* pb = B; - decimal[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (decimal* B = b) + for (int i = 0; i < a.Length; i++) + { + decimal* pb = B; + decimal[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -83059,7 +84125,7 @@ public static unsafe double[][] DotWithTransposed(this decimal[][] a, decimal[,] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this decimal[,] a, decimal[][] b, double[][] result) + public static double[][] DotWithTransposed(this decimal[,] a, decimal[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -83069,20 +84135,22 @@ public static unsafe double[][] DotWithTransposed(this decimal[,] a, decimal[][] #endif int n = a.Rows(); - fixed (decimal* A = a) - for (int j = 0; j < b.Length; j++) - { - decimal* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - decimal[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < b.Length; j++) + { + decimal* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + decimal[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -83228,7 +84296,7 @@ public static double[][] DotWithTransposed(this decimal[][] a, decimal[] columnV #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this decimal[,] a, decimal[,] b, double[,] result) + public static double[,] TransposeAndDot(this decimal[,] a, decimal[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -83239,35 +84307,37 @@ public static double[][] DotWithTransposed(this decimal[][] a, decimal[] columnV throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (decimal* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (decimal* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - decimal* pb = B; - - for (int k = 0; k < n; k++) - { - decimal aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + decimal* pb = B; + + for (int k = 0; k < n; k++) + { + decimal aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -83661,18 +84731,22 @@ public static double[][] DivideByDiagonal(this decimal[][] a, decimal[] diagonal #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this decimal[] a, decimal[] b, double[,] result) + public static double[,] Outer(this decimal[] a, decimal[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -83835,25 +84909,28 @@ public static double[][] Kronecker(this decimal[][] a, decimal[][] b, double[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this decimal[][] a, decimal[,] b, double[][] result) + public static double[][] Kronecker(this decimal[][] a, decimal[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (decimal* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - decimal* pb = B; + unsafe + { + fixed (decimal* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + decimal* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -83907,18 +84984,22 @@ public static double[][] Kronecker(this decimal[,] a, decimal[][] b, double[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this decimal[] a, decimal[] b, double[] result) + public static double[] Kronecker(this decimal[] a, decimal[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -84112,7 +85193,7 @@ public static int[][] Dot(this decimal[][] a, decimal[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this decimal[,] a, decimal[][] b, int[][] result) + public static int[][] Dot(this decimal[,] a, decimal[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -84126,21 +85207,24 @@ public static unsafe int[][] Dot(this decimal[,] a, decimal[][] b, int[][] resul var t = new decimal[K]; - fixed (decimal* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - decimal* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + decimal* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -84218,7 +85302,7 @@ public static int[] Dot(this decimal[][] matrix, decimal[] columnVector, int[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this decimal[,] matrix, decimal[] columnVector, int[] result) + public static int[] Dot(this decimal[,] matrix, decimal[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -84227,55 +85311,54 @@ public static unsafe int[] Dot(this decimal[,] matrix, decimal[] columnVector, i if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (decimal* a = matrix) + fixed (decimal* x = columnVector) + fixed (int* r = result) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + int* pr = r; - fixed (decimal* a = matrix) - fixed (decimal* x = columnVector) - fixed (int* r = result) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - decimal* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + decimal* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - decimal* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + decimal* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -84381,7 +85464,7 @@ public static int[] Dot(this decimal[] rowVector, decimal[][] matrix, int[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this decimal[][] a, decimal[,] b, int[][] result) + public static int[][] DotWithTransposed(this decimal[][] a, decimal[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -84391,19 +85474,22 @@ public static unsafe int[][] DotWithTransposed(this decimal[][] a, decimal[,] b, #endif int n = b.Rows(); - fixed (decimal* B = b) - for (int i = 0; i < a.Length; i++) - { - decimal* pb = B; - decimal[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (decimal* B = b) + for (int i = 0; i < a.Length; i++) + { + decimal* pb = B; + decimal[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -84425,7 +85511,7 @@ public static unsafe int[][] DotWithTransposed(this decimal[][] a, decimal[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this decimal[,] a, decimal[][] b, int[][] result) + public static int[][] DotWithTransposed(this decimal[,] a, decimal[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -84435,20 +85521,22 @@ public static unsafe int[][] DotWithTransposed(this decimal[,] a, decimal[][] b, #endif int n = a.Rows(); - fixed (decimal* A = a) - for (int j = 0; j < b.Length; j++) - { - decimal* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - decimal[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < b.Length; j++) + { + decimal* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + decimal[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -84594,7 +85682,7 @@ public static int[][] DotWithTransposed(this decimal[][] a, decimal[] columnVect #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this decimal[,] a, decimal[,] b, int[,] result) + public static int[,] TransposeAndDot(this decimal[,] a, decimal[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -84605,35 +85693,37 @@ public static int[][] DotWithTransposed(this decimal[][] a, decimal[] columnVect throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (decimal* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (decimal* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - decimal* pb = B; - - for (int k = 0; k < n; k++) - { - decimal aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + decimal* pb = B; + + for (int k = 0; k < n; k++) + { + decimal aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -85027,18 +86117,22 @@ public static int[][] DivideByDiagonal(this decimal[][] a, decimal[] diagonal, i #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this decimal[] a, decimal[] b, int[,] result) + public static int[,] Outer(this decimal[] a, decimal[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -85201,25 +86295,28 @@ public static int[][] Kronecker(this decimal[][] a, decimal[][] b, int[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this decimal[][] a, decimal[,] b, int[][] result) + public static int[][] Kronecker(this decimal[][] a, decimal[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (decimal* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - decimal* pb = B; + unsafe + { + fixed (decimal* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + decimal* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -85273,18 +86370,22 @@ public static int[][] Kronecker(this decimal[,] a, decimal[][] b, int[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this decimal[] a, decimal[] b, int[] result) + public static int[] Kronecker(this decimal[] a, decimal[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -85478,7 +86579,7 @@ public static decimal[][] Dot(this decimal[][] a, double[,] b, decimal[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] Dot(this decimal[,] a, double[][] b, decimal[][] result) + public static decimal[][] Dot(this decimal[,] a, double[][] b, decimal[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -85492,21 +86593,24 @@ public static unsafe decimal[][] Dot(this decimal[,] a, double[][] b, decimal[][ var t = new double[K]; - fixed (decimal* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - decimal* pa = A; - for (int i = 0; i < N; i++) - { - decimal s = (decimal)0; - for (int k = 0; k < t.Length; k++) - s += (decimal)((decimal)(*pa++) * (decimal)t[k]); - result[i][j] = (decimal)s; - } - } + decimal* pa = A; + for (int i = 0; i < N; i++) + { + decimal s = (decimal)0; + for (int k = 0; k < t.Length; k++) + s += (decimal)((decimal)(*pa++) * (decimal)t[k]); + result[i][j] = (decimal)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -85584,7 +86688,7 @@ public static decimal[] Dot(this decimal[][] matrix, double[] columnVector, deci #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[] Dot(this decimal[,] matrix, double[] columnVector, decimal[] result) + public static decimal[] Dot(this decimal[,] matrix, double[] columnVector, decimal[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -85593,55 +86697,54 @@ public static unsafe decimal[] Dot(this decimal[,] matrix, double[] columnVector if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (decimal* a = matrix) + fixed (double* x = columnVector) + fixed (decimal* r = result) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + decimal* pr = r; - fixed (decimal* a = matrix) - fixed (double* x = columnVector) - fixed (decimal* r = result) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - decimal sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (decimal)((decimal)(*pa1++) * (decimal)(*px)); - sum2 += (decimal)((decimal)(*pa2++) * (decimal)(*px)); - px++; - } - - *pr++ = (decimal)sum1; - *pr++ = (decimal)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + decimal sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (decimal)((decimal)(*pa1++) * (decimal)(*px)); + sum2 += (decimal)((decimal)(*pa2++) * (decimal)(*px)); + px++; + } + + *pr++ = (decimal)sum1; + *pr++ = (decimal)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - decimal sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + decimal sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (decimal)((decimal)(*pa1++) * (decimal)(*px++)); + for (int j = 0; j < cols; j++) + sum += (decimal)((decimal)(*pa1++) * (decimal)(*px++)); - *pr = (decimal)sum; - } - } + *pr = (decimal)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -85747,7 +86850,7 @@ public static decimal[] Dot(this decimal[] rowVector, double[][] matrix, decimal #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] DotWithTransposed(this decimal[][] a, double[,] b, decimal[][] result) + public static decimal[][] DotWithTransposed(this decimal[][] a, double[,] b, decimal[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -85757,19 +86860,22 @@ public static unsafe decimal[][] DotWithTransposed(this decimal[][] a, double[,] #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - decimal[] arow = a[i]; - for (int j = 0; j < n; j++) - { - decimal sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (decimal)((decimal)arow[k] * (decimal)(*pb++)); - result[i][j] = (decimal)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + decimal[] arow = a[i]; + for (int j = 0; j < n; j++) + { + decimal sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (decimal)((decimal)arow[k] * (decimal)(*pb++)); + result[i][j] = (decimal)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -85791,7 +86897,7 @@ public static unsafe decimal[][] DotWithTransposed(this decimal[][] a, double[,] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] DotWithTransposed(this decimal[,] a, double[][] b, decimal[][] result) + public static decimal[][] DotWithTransposed(this decimal[,] a, double[][] b, decimal[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -85801,20 +86907,22 @@ public static unsafe decimal[][] DotWithTransposed(this decimal[,] a, double[][] #endif int n = a.Rows(); - fixed (decimal* A = a) - for (int j = 0; j < b.Length; j++) - { - decimal* pa = A; - for (int i = 0; i < n; i++) - { - decimal sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (decimal)((decimal)(*pa++) * (decimal)brow[k]); - result[i][j] = (decimal)sum; - } - } - + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < b.Length; j++) + { + decimal* pa = A; + for (int i = 0; i < n; i++) + { + decimal sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (decimal)((decimal)(*pa++) * (decimal)brow[k]); + result[i][j] = (decimal)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -85960,7 +87068,7 @@ public static decimal[][] DotWithTransposed(this decimal[][] a, double[] columnV #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[,] TransposeAndDot(this decimal[,] a, double[,] b, decimal[,] result) + public static decimal[,] TransposeAndDot(this decimal[,] a, double[,] b, decimal[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -85971,35 +87079,37 @@ public static decimal[][] DotWithTransposed(this decimal[][] a, double[] columnV throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (decimal* R = result) + fixed (double* B = b) + fixed (decimal* ptemp = new decimal[p]) + { + decimal* pr = R; - fixed (decimal* R = result) - fixed (double* B = b) - fixed (decimal* ptemp = new decimal[p]) - { - decimal* pr = R; - - for (int i = 0; i < m; i++) - { - decimal* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - decimal aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (decimal)((decimal)aval * (decimal)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (decimal)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + decimal* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + decimal aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (decimal)((decimal)aval * (decimal)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (decimal)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -86393,18 +87503,22 @@ public static decimal[][] DivideByDiagonal(this decimal[][] a, double[] diagonal #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[,] Outer(this decimal[] a, double[] b, decimal[,] result) + public static decimal[,] Outer(this decimal[] a, double[] b, decimal[,] result) { - fixed (decimal* R = result) - { - decimal* pr = R; - for (int i = 0; i < a.Length; i++) - { - decimal x = (decimal)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (decimal)(x * (decimal)b[j]); - } - } + unsafe + { + fixed (decimal* R = result) + { + decimal* pr = R; + for (int i = 0; i < a.Length; i++) + { + decimal x = (decimal)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (decimal)(x * (decimal)b[j]); + } + } + } + return result; } @@ -86567,25 +87681,28 @@ public static decimal[][] Kronecker(this decimal[][] a, double[][] b, decimal[][ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] Kronecker(this decimal[][] a, double[,] b, decimal[][] result) + public static decimal[][] Kronecker(this decimal[][] a, double[,] b, decimal[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - decimal aval = (decimal)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + decimal aval = (decimal)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (decimal)(aval * (decimal)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (decimal)(aval * (decimal)(*pb++)); + } + } + return result; } @@ -86639,18 +87756,22 @@ public static decimal[][] Kronecker(this decimal[,] a, double[][] b, decimal[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[] Kronecker(this decimal[] a, double[] b, decimal[] result) + public static decimal[] Kronecker(this decimal[] a, double[] b, decimal[] result) { - fixed (decimal* R = result) - { - decimal* pr = R; - for (int i = 0; i < a.Length; i++) - { - decimal x = (decimal)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (decimal)(x * (decimal)b[j]); - } - } + unsafe + { + fixed (decimal* R = result) + { + decimal* pr = R; + for (int i = 0; i < a.Length; i++) + { + decimal x = (decimal)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (decimal)(x * (decimal)b[j]); + } + } + } + return result; } @@ -86844,7 +87965,7 @@ public static double[][] Dot(this decimal[][] a, double[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this decimal[,] a, double[][] b, double[][] result) + public static double[][] Dot(this decimal[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -86858,21 +87979,24 @@ public static unsafe double[][] Dot(this decimal[,] a, double[][] b, double[][] var t = new double[K]; - fixed (decimal* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - decimal* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + decimal* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -86950,7 +88074,7 @@ public static double[] Dot(this decimal[][] matrix, double[] columnVector, doubl #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this decimal[,] matrix, double[] columnVector, double[] result) + public static double[] Dot(this decimal[,] matrix, double[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -86959,55 +88083,54 @@ public static unsafe double[] Dot(this decimal[,] matrix, double[] columnVector, if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (decimal* a = matrix) + fixed (double* x = columnVector) + fixed (double* r = result) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + double* pr = r; - fixed (decimal* a = matrix) - fixed (double* x = columnVector) - fixed (double* r = result) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -87113,7 +88236,7 @@ public static double[] Dot(this decimal[] rowVector, double[][] matrix, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this decimal[][] a, double[,] b, double[][] result) + public static double[][] DotWithTransposed(this decimal[][] a, double[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -87123,19 +88246,22 @@ public static unsafe double[][] DotWithTransposed(this decimal[][] a, double[,] #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - decimal[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + decimal[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -87157,7 +88283,7 @@ public static unsafe double[][] DotWithTransposed(this decimal[][] a, double[,] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this decimal[,] a, double[][] b, double[][] result) + public static double[][] DotWithTransposed(this decimal[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -87167,20 +88293,22 @@ public static unsafe double[][] DotWithTransposed(this decimal[,] a, double[][] #endif int n = a.Rows(); - fixed (decimal* A = a) - for (int j = 0; j < b.Length; j++) - { - decimal* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < b.Length; j++) + { + decimal* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -87326,7 +88454,7 @@ public static double[][] DotWithTransposed(this decimal[][] a, double[] columnVe #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this decimal[,] a, double[,] b, double[,] result) + public static double[,] TransposeAndDot(this decimal[,] a, double[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -87337,35 +88465,37 @@ public static double[][] DotWithTransposed(this decimal[][] a, double[] columnVe throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (double* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (double* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - decimal aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + decimal aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -87759,18 +88889,22 @@ public static double[][] DivideByDiagonal(this decimal[][] a, double[] diagonal, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this decimal[] a, double[] b, double[,] result) + public static double[,] Outer(this decimal[] a, double[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -87933,25 +89067,28 @@ public static double[][] Kronecker(this decimal[][] a, double[][] b, double[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this decimal[][] a, double[,] b, double[][] result) + public static double[][] Kronecker(this decimal[][] a, double[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -88005,18 +89142,22 @@ public static double[][] Kronecker(this decimal[,] a, double[][] b, double[][] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this decimal[] a, double[] b, double[] result) + public static double[] Kronecker(this decimal[] a, double[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -88210,7 +89351,7 @@ public static int[][] Dot(this decimal[][] a, double[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this decimal[,] a, double[][] b, int[][] result) + public static int[][] Dot(this decimal[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -88224,21 +89365,24 @@ public static unsafe int[][] Dot(this decimal[,] a, double[][] b, int[][] result var t = new double[K]; - fixed (decimal* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - decimal* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + decimal* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -88316,7 +89460,7 @@ public static int[] Dot(this decimal[][] matrix, double[] columnVector, int[] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this decimal[,] matrix, double[] columnVector, int[] result) + public static int[] Dot(this decimal[,] matrix, double[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -88325,55 +89469,54 @@ public static unsafe int[] Dot(this decimal[,] matrix, double[] columnVector, in if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (decimal* a = matrix) + fixed (double* x = columnVector) + fixed (int* r = result) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + int* pr = r; - fixed (decimal* a = matrix) - fixed (double* x = columnVector) - fixed (int* r = result) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -88479,7 +89622,7 @@ public static int[] Dot(this decimal[] rowVector, double[][] matrix, int[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this decimal[][] a, double[,] b, int[][] result) + public static int[][] DotWithTransposed(this decimal[][] a, double[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -88489,19 +89632,22 @@ public static unsafe int[][] DotWithTransposed(this decimal[][] a, double[,] b, #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - decimal[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + decimal[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -88523,7 +89669,7 @@ public static unsafe int[][] DotWithTransposed(this decimal[][] a, double[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this decimal[,] a, double[][] b, int[][] result) + public static int[][] DotWithTransposed(this decimal[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -88533,20 +89679,22 @@ public static unsafe int[][] DotWithTransposed(this decimal[,] a, double[][] b, #endif int n = a.Rows(); - fixed (decimal* A = a) - for (int j = 0; j < b.Length; j++) - { - decimal* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < b.Length; j++) + { + decimal* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -88692,7 +89840,7 @@ public static int[][] DotWithTransposed(this decimal[][] a, double[] columnVecto #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this decimal[,] a, double[,] b, int[,] result) + public static int[,] TransposeAndDot(this decimal[,] a, double[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -88703,35 +89851,37 @@ public static int[][] DotWithTransposed(this decimal[][] a, double[] columnVecto throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (double* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (double* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - decimal aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + decimal aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -89125,18 +90275,22 @@ public static int[][] DivideByDiagonal(this decimal[][] a, double[] diagonal, in #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this decimal[] a, double[] b, int[,] result) + public static int[,] Outer(this decimal[] a, double[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -89299,25 +90453,28 @@ public static int[][] Kronecker(this decimal[][] a, double[][] b, int[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this decimal[][] a, double[,] b, int[][] result) + public static int[][] Kronecker(this decimal[][] a, double[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -89371,18 +90528,22 @@ public static int[][] Kronecker(this decimal[,] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this decimal[] a, double[] b, int[] result) + public static int[] Kronecker(this decimal[] a, double[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -89576,7 +90737,7 @@ public static decimal[][] Dot(this decimal[][] a, int[,] b, decimal[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] Dot(this decimal[,] a, int[][] b, decimal[][] result) + public static decimal[][] Dot(this decimal[,] a, int[][] b, decimal[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -89590,21 +90751,24 @@ public static unsafe decimal[][] Dot(this decimal[,] a, int[][] b, decimal[][] r var t = new int[K]; - fixed (decimal* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - decimal* pa = A; - for (int i = 0; i < N; i++) - { - decimal s = (decimal)0; - for (int k = 0; k < t.Length; k++) - s += (decimal)((decimal)(*pa++) * (decimal)t[k]); - result[i][j] = (decimal)s; - } - } + decimal* pa = A; + for (int i = 0; i < N; i++) + { + decimal s = (decimal)0; + for (int k = 0; k < t.Length; k++) + s += (decimal)((decimal)(*pa++) * (decimal)t[k]); + result[i][j] = (decimal)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -89682,7 +90846,7 @@ public static decimal[] Dot(this decimal[][] matrix, int[] columnVector, decimal #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[] Dot(this decimal[,] matrix, int[] columnVector, decimal[] result) + public static decimal[] Dot(this decimal[,] matrix, int[] columnVector, decimal[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -89691,55 +90855,54 @@ public static unsafe decimal[] Dot(this decimal[,] matrix, int[] columnVector, d if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (decimal* a = matrix) + fixed (int* x = columnVector) + fixed (decimal* r = result) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + decimal* pr = r; - fixed (decimal* a = matrix) - fixed (int* x = columnVector) - fixed (decimal* r = result) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - decimal sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (decimal)((decimal)(*pa1++) * (decimal)(*px)); - sum2 += (decimal)((decimal)(*pa2++) * (decimal)(*px)); - px++; - } - - *pr++ = (decimal)sum1; - *pr++ = (decimal)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + decimal sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (decimal)((decimal)(*pa1++) * (decimal)(*px)); + sum2 += (decimal)((decimal)(*pa2++) * (decimal)(*px)); + px++; + } + + *pr++ = (decimal)sum1; + *pr++ = (decimal)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - decimal sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + decimal sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (decimal)((decimal)(*pa1++) * (decimal)(*px++)); + for (int j = 0; j < cols; j++) + sum += (decimal)((decimal)(*pa1++) * (decimal)(*px++)); - *pr = (decimal)sum; - } - } + *pr = (decimal)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -89845,7 +91008,7 @@ public static decimal[] Dot(this decimal[] rowVector, int[][] matrix, decimal[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] DotWithTransposed(this decimal[][] a, int[,] b, decimal[][] result) + public static decimal[][] DotWithTransposed(this decimal[][] a, int[,] b, decimal[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -89855,19 +91018,22 @@ public static unsafe decimal[][] DotWithTransposed(this decimal[][] a, int[,] b, #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - decimal[] arow = a[i]; - for (int j = 0; j < n; j++) - { - decimal sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (decimal)((decimal)arow[k] * (decimal)(*pb++)); - result[i][j] = (decimal)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + decimal[] arow = a[i]; + for (int j = 0; j < n; j++) + { + decimal sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (decimal)((decimal)arow[k] * (decimal)(*pb++)); + result[i][j] = (decimal)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -89889,7 +91055,7 @@ public static unsafe decimal[][] DotWithTransposed(this decimal[][] a, int[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] DotWithTransposed(this decimal[,] a, int[][] b, decimal[][] result) + public static decimal[][] DotWithTransposed(this decimal[,] a, int[][] b, decimal[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -89899,20 +91065,22 @@ public static unsafe decimal[][] DotWithTransposed(this decimal[,] a, int[][] b, #endif int n = a.Rows(); - fixed (decimal* A = a) - for (int j = 0; j < b.Length; j++) - { - decimal* pa = A; - for (int i = 0; i < n; i++) - { - decimal sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (decimal)((decimal)(*pa++) * (decimal)brow[k]); - result[i][j] = (decimal)sum; - } - } - + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < b.Length; j++) + { + decimal* pa = A; + for (int i = 0; i < n; i++) + { + decimal sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (decimal)((decimal)(*pa++) * (decimal)brow[k]); + result[i][j] = (decimal)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -90058,7 +91226,7 @@ public static decimal[][] DotWithTransposed(this decimal[][] a, int[] columnVect #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[,] TransposeAndDot(this decimal[,] a, int[,] b, decimal[,] result) + public static decimal[,] TransposeAndDot(this decimal[,] a, int[,] b, decimal[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -90069,35 +91237,37 @@ public static decimal[][] DotWithTransposed(this decimal[][] a, int[] columnVect throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (decimal* R = result) + fixed (int* B = b) + fixed (decimal* ptemp = new decimal[p]) + { + decimal* pr = R; - fixed (decimal* R = result) - fixed (int* B = b) - fixed (decimal* ptemp = new decimal[p]) - { - decimal* pr = R; - - for (int i = 0; i < m; i++) - { - decimal* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - decimal aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (decimal)((decimal)aval * (decimal)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (decimal)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + decimal* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + decimal aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (decimal)((decimal)aval * (decimal)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (decimal)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -90491,18 +91661,22 @@ public static decimal[][] DivideByDiagonal(this decimal[][] a, int[] diagonal, d #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[,] Outer(this decimal[] a, int[] b, decimal[,] result) + public static decimal[,] Outer(this decimal[] a, int[] b, decimal[,] result) { - fixed (decimal* R = result) - { - decimal* pr = R; - for (int i = 0; i < a.Length; i++) - { - decimal x = (decimal)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (decimal)(x * (decimal)b[j]); - } - } + unsafe + { + fixed (decimal* R = result) + { + decimal* pr = R; + for (int i = 0; i < a.Length; i++) + { + decimal x = (decimal)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (decimal)(x * (decimal)b[j]); + } + } + } + return result; } @@ -90665,25 +91839,28 @@ public static decimal[][] Kronecker(this decimal[][] a, int[][] b, decimal[][] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] Kronecker(this decimal[][] a, int[,] b, decimal[][] result) + public static decimal[][] Kronecker(this decimal[][] a, int[,] b, decimal[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - decimal aval = (decimal)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + decimal aval = (decimal)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (decimal)(aval * (decimal)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (decimal)(aval * (decimal)(*pb++)); + } + } + return result; } @@ -90737,18 +91914,22 @@ public static decimal[][] Kronecker(this decimal[,] a, int[][] b, decimal[][] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[] Kronecker(this decimal[] a, int[] b, decimal[] result) + public static decimal[] Kronecker(this decimal[] a, int[] b, decimal[] result) { - fixed (decimal* R = result) - { - decimal* pr = R; - for (int i = 0; i < a.Length; i++) - { - decimal x = (decimal)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (decimal)(x * (decimal)b[j]); - } - } + unsafe + { + fixed (decimal* R = result) + { + decimal* pr = R; + for (int i = 0; i < a.Length; i++) + { + decimal x = (decimal)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (decimal)(x * (decimal)b[j]); + } + } + } + return result; } @@ -90942,7 +92123,7 @@ public static int[][] Dot(this decimal[][] a, int[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this decimal[,] a, int[][] b, int[][] result) + public static int[][] Dot(this decimal[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -90956,21 +92137,24 @@ public static unsafe int[][] Dot(this decimal[,] a, int[][] b, int[][] result) var t = new int[K]; - fixed (decimal* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - decimal* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + decimal* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -91048,7 +92232,7 @@ public static int[] Dot(this decimal[][] matrix, int[] columnVector, int[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this decimal[,] matrix, int[] columnVector, int[] result) + public static int[] Dot(this decimal[,] matrix, int[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -91057,55 +92241,54 @@ public static unsafe int[] Dot(this decimal[,] matrix, int[] columnVector, int[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (decimal* a = matrix) + fixed (int* x = columnVector) + fixed (int* r = result) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + int* pr = r; - fixed (decimal* a = matrix) - fixed (int* x = columnVector) - fixed (int* r = result) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -91211,7 +92394,7 @@ public static int[] Dot(this decimal[] rowVector, int[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this decimal[][] a, int[,] b, int[][] result) + public static int[][] DotWithTransposed(this decimal[][] a, int[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -91221,19 +92404,22 @@ public static unsafe int[][] DotWithTransposed(this decimal[][] a, int[,] b, int #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - decimal[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + decimal[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -91255,7 +92441,7 @@ public static unsafe int[][] DotWithTransposed(this decimal[][] a, int[,] b, int #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this decimal[,] a, int[][] b, int[][] result) + public static int[][] DotWithTransposed(this decimal[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -91265,20 +92451,22 @@ public static unsafe int[][] DotWithTransposed(this decimal[,] a, int[][] b, int #endif int n = a.Rows(); - fixed (decimal* A = a) - for (int j = 0; j < b.Length; j++) - { - decimal* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < b.Length; j++) + { + decimal* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -91424,7 +92612,7 @@ public static int[][] DotWithTransposed(this decimal[][] a, int[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this decimal[,] a, int[,] b, int[,] result) + public static int[,] TransposeAndDot(this decimal[,] a, int[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -91435,35 +92623,37 @@ public static int[][] DotWithTransposed(this decimal[][] a, int[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (int* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (int* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - decimal aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + decimal aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -91857,18 +93047,22 @@ public static int[][] DivideByDiagonal(this decimal[][] a, int[] diagonal, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this decimal[] a, int[] b, int[,] result) + public static int[,] Outer(this decimal[] a, int[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -92031,25 +93225,28 @@ public static int[][] Kronecker(this decimal[][] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this decimal[][] a, int[,] b, int[][] result) + public static int[][] Kronecker(this decimal[][] a, int[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -92103,18 +93300,22 @@ public static int[][] Kronecker(this decimal[,] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this decimal[] a, int[] b, int[] result) + public static int[] Kronecker(this decimal[] a, int[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -92308,7 +93509,7 @@ public static double[][] Dot(this decimal[][] a, int[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this decimal[,] a, int[][] b, double[][] result) + public static double[][] Dot(this decimal[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -92322,21 +93523,24 @@ public static unsafe double[][] Dot(this decimal[,] a, int[][] b, double[][] res var t = new int[K]; - fixed (decimal* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - decimal* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + decimal* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -92414,7 +93618,7 @@ public static double[] Dot(this decimal[][] matrix, int[] columnVector, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this decimal[,] matrix, int[] columnVector, double[] result) + public static double[] Dot(this decimal[,] matrix, int[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -92423,55 +93627,54 @@ public static unsafe double[] Dot(this decimal[,] matrix, int[] columnVector, do if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (decimal* a = matrix) + fixed (int* x = columnVector) + fixed (double* r = result) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + double* pr = r; - fixed (decimal* a = matrix) - fixed (int* x = columnVector) - fixed (double* r = result) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -92577,7 +93780,7 @@ public static double[] Dot(this decimal[] rowVector, int[][] matrix, double[] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this decimal[][] a, int[,] b, double[][] result) + public static double[][] DotWithTransposed(this decimal[][] a, int[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -92587,19 +93790,22 @@ public static unsafe double[][] DotWithTransposed(this decimal[][] a, int[,] b, #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - decimal[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + decimal[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -92621,7 +93827,7 @@ public static unsafe double[][] DotWithTransposed(this decimal[][] a, int[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this decimal[,] a, int[][] b, double[][] result) + public static double[][] DotWithTransposed(this decimal[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -92631,20 +93837,22 @@ public static unsafe double[][] DotWithTransposed(this decimal[,] a, int[][] b, #endif int n = a.Rows(); - fixed (decimal* A = a) - for (int j = 0; j < b.Length; j++) - { - decimal* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < b.Length; j++) + { + decimal* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -92790,7 +93998,7 @@ public static double[][] DotWithTransposed(this decimal[][] a, int[] columnVecto #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this decimal[,] a, int[,] b, double[,] result) + public static double[,] TransposeAndDot(this decimal[,] a, int[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -92801,35 +94009,37 @@ public static double[][] DotWithTransposed(this decimal[][] a, int[] columnVecto throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (int* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (int* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - decimal aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + decimal aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -93223,18 +94433,22 @@ public static double[][] DivideByDiagonal(this decimal[][] a, int[] diagonal, do #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this decimal[] a, int[] b, double[,] result) + public static double[,] Outer(this decimal[] a, int[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -93397,25 +94611,28 @@ public static double[][] Kronecker(this decimal[][] a, int[][] b, double[][] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this decimal[][] a, int[,] b, double[][] result) + public static double[][] Kronecker(this decimal[][] a, int[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -93469,18 +94686,22 @@ public static double[][] Kronecker(this decimal[,] a, int[][] b, double[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this decimal[] a, int[] b, double[] result) + public static double[] Kronecker(this decimal[] a, int[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -93674,7 +94895,7 @@ public static decimal[][] Dot(this decimal[][] a, float[,] b, decimal[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] Dot(this decimal[,] a, float[][] b, decimal[][] result) + public static decimal[][] Dot(this decimal[,] a, float[][] b, decimal[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -93688,21 +94909,24 @@ public static unsafe decimal[][] Dot(this decimal[,] a, float[][] b, decimal[][] var t = new float[K]; - fixed (decimal* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - decimal* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (decimal)s; - } - } + decimal* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (decimal)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -93780,7 +95004,7 @@ public static decimal[] Dot(this decimal[][] matrix, float[] columnVector, decim #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[] Dot(this decimal[,] matrix, float[] columnVector, decimal[] result) + public static decimal[] Dot(this decimal[,] matrix, float[] columnVector, decimal[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -93789,55 +95013,54 @@ public static unsafe decimal[] Dot(this decimal[,] matrix, float[] columnVector, if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (decimal* a = matrix) + fixed (float* x = columnVector) + fixed (decimal* r = result) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + decimal* pr = r; - fixed (decimal* a = matrix) - fixed (float* x = columnVector) - fixed (decimal* r = result) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - decimal* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (decimal)sum1; - *pr++ = (decimal)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (decimal)sum1; + *pr++ = (decimal)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (decimal)sum; - } - } + *pr = (decimal)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -93943,7 +95166,7 @@ public static decimal[] Dot(this decimal[] rowVector, float[][] matrix, decimal[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] DotWithTransposed(this decimal[][] a, float[,] b, decimal[][] result) + public static decimal[][] DotWithTransposed(this decimal[][] a, float[,] b, decimal[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -93953,19 +95176,22 @@ public static unsafe decimal[][] DotWithTransposed(this decimal[][] a, float[,] #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - decimal[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (decimal)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + decimal[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (decimal)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -93987,7 +95213,7 @@ public static unsafe decimal[][] DotWithTransposed(this decimal[][] a, float[,] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] DotWithTransposed(this decimal[,] a, float[][] b, decimal[][] result) + public static decimal[][] DotWithTransposed(this decimal[,] a, float[][] b, decimal[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -93997,20 +95223,22 @@ public static unsafe decimal[][] DotWithTransposed(this decimal[,] a, float[][] #endif int n = a.Rows(); - fixed (decimal* A = a) - for (int j = 0; j < b.Length; j++) - { - decimal* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (decimal)sum; - } - } - + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < b.Length; j++) + { + decimal* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (decimal)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -94156,7 +95384,7 @@ public static decimal[][] DotWithTransposed(this decimal[][] a, float[] columnVe #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[,] TransposeAndDot(this decimal[,] a, float[,] b, decimal[,] result) + public static decimal[,] TransposeAndDot(this decimal[,] a, float[,] b, decimal[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -94167,35 +95395,37 @@ public static decimal[][] DotWithTransposed(this decimal[][] a, float[] columnVe throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (decimal* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + decimal* pr = R; - fixed (decimal* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - decimal* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - decimal aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (decimal)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + decimal aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (decimal)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -94589,18 +95819,22 @@ public static decimal[][] DivideByDiagonal(this decimal[][] a, float[] diagonal, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[,] Outer(this decimal[] a, float[] b, decimal[,] result) + public static decimal[,] Outer(this decimal[] a, float[] b, decimal[,] result) { - fixed (decimal* R = result) - { - decimal* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (decimal)(x * (double)b[j]); - } - } + unsafe + { + fixed (decimal* R = result) + { + decimal* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (decimal)(x * (double)b[j]); + } + } + } + return result; } @@ -94763,25 +95997,28 @@ public static decimal[][] Kronecker(this decimal[][] a, float[][] b, decimal[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[][] Kronecker(this decimal[][] a, float[,] b, decimal[][] result) + public static decimal[][] Kronecker(this decimal[][] a, float[,] b, decimal[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (decimal)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (decimal)(aval * (double)(*pb++)); + } + } + return result; } @@ -94835,18 +96072,22 @@ public static decimal[][] Kronecker(this decimal[,] a, float[][] b, decimal[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe decimal[] Kronecker(this decimal[] a, float[] b, decimal[] result) + public static decimal[] Kronecker(this decimal[] a, float[] b, decimal[] result) { - fixed (decimal* R = result) - { - decimal* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (decimal)(x * (double)b[j]); - } - } + unsafe + { + fixed (decimal* R = result) + { + decimal* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (decimal)(x * (double)b[j]); + } + } + } + return result; } @@ -95040,7 +96281,7 @@ public static float[][] Dot(this decimal[][] a, float[,] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Dot(this decimal[,] a, float[][] b, float[][] result) + public static float[][] Dot(this decimal[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -95054,21 +96295,24 @@ public static unsafe float[][] Dot(this decimal[,] a, float[][] b, float[][] res var t = new float[K]; - fixed (decimal* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - decimal* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (float)s; - } - } + decimal* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (float)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -95146,7 +96390,7 @@ public static float[] Dot(this decimal[][] matrix, float[] columnVector, float[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Dot(this decimal[,] matrix, float[] columnVector, float[] result) + public static float[] Dot(this decimal[,] matrix, float[] columnVector, float[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -95155,55 +96399,54 @@ public static unsafe float[] Dot(this decimal[,] matrix, float[] columnVector, f if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (decimal* a = matrix) + fixed (float* x = columnVector) + fixed (float* r = result) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + float* pr = r; - fixed (decimal* a = matrix) - fixed (float* x = columnVector) - fixed (float* r = result) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (float)sum1; - *pr++ = (float)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (float)sum1; + *pr++ = (float)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (float)sum; - } - } + *pr = (float)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -95309,7 +96552,7 @@ public static float[] Dot(this decimal[] rowVector, float[][] matrix, float[] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this decimal[][] a, float[,] b, float[][] result) + public static float[][] DotWithTransposed(this decimal[][] a, float[,] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -95319,19 +96562,22 @@ public static unsafe float[][] DotWithTransposed(this decimal[][] a, float[,] b, #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - decimal[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (float)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + decimal[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -95353,7 +96599,7 @@ public static unsafe float[][] DotWithTransposed(this decimal[][] a, float[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this decimal[,] a, float[][] b, float[][] result) + public static float[][] DotWithTransposed(this decimal[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -95363,20 +96609,22 @@ public static unsafe float[][] DotWithTransposed(this decimal[,] a, float[][] b, #endif int n = a.Rows(); - fixed (decimal* A = a) - for (int j = 0; j < b.Length; j++) - { - decimal* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (float)sum; - } - } - + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < b.Length; j++) + { + decimal* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -95522,7 +96770,7 @@ public static float[][] DotWithTransposed(this decimal[][] a, float[] columnVect #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] TransposeAndDot(this decimal[,] a, float[,] b, float[,] result) + public static float[,] TransposeAndDot(this decimal[,] a, float[,] b, float[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -95533,35 +96781,37 @@ public static float[][] DotWithTransposed(this decimal[][] a, float[] columnVect throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (float* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + float* pr = R; - fixed (float* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - float* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - decimal aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (float)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + decimal aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (float)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -95955,18 +97205,22 @@ public static float[][] DivideByDiagonal(this decimal[][] a, float[] diagonal, f #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] Outer(this decimal[] a, float[] b, float[,] result) + public static float[,] Outer(this decimal[] a, float[] b, float[,] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -96129,25 +97383,28 @@ public static float[][] Kronecker(this decimal[][] a, float[][] b, float[][] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Kronecker(this decimal[][] a, float[,] b, float[][] result) + public static float[][] Kronecker(this decimal[][] a, float[,] b, float[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); + } + } + return result; } @@ -96201,18 +97458,22 @@ public static float[][] Kronecker(this decimal[,] a, float[][] b, float[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Kronecker(this decimal[] a, float[] b, float[] result) + public static float[] Kronecker(this decimal[] a, float[] b, float[] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -96406,7 +97667,7 @@ public static double[][] Dot(this decimal[][] a, float[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this decimal[,] a, float[][] b, double[][] result) + public static double[][] Dot(this decimal[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -96420,21 +97681,24 @@ public static unsafe double[][] Dot(this decimal[,] a, float[][] b, double[][] r var t = new float[K]; - fixed (decimal* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - decimal* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + decimal* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -96512,7 +97776,7 @@ public static double[] Dot(this decimal[][] matrix, float[] columnVector, double #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this decimal[,] matrix, float[] columnVector, double[] result) + public static double[] Dot(this decimal[,] matrix, float[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -96521,55 +97785,54 @@ public static unsafe double[] Dot(this decimal[,] matrix, float[] columnVector, if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (decimal* a = matrix) + fixed (float* x = columnVector) + fixed (double* r = result) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + double* pr = r; - fixed (decimal* a = matrix) - fixed (float* x = columnVector) - fixed (double* r = result) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -96675,7 +97938,7 @@ public static double[] Dot(this decimal[] rowVector, float[][] matrix, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this decimal[][] a, float[,] b, double[][] result) + public static double[][] DotWithTransposed(this decimal[][] a, float[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -96685,19 +97948,22 @@ public static unsafe double[][] DotWithTransposed(this decimal[][] a, float[,] b #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - decimal[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + decimal[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -96719,7 +97985,7 @@ public static unsafe double[][] DotWithTransposed(this decimal[][] a, float[,] b #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this decimal[,] a, float[][] b, double[][] result) + public static double[][] DotWithTransposed(this decimal[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -96729,20 +97995,22 @@ public static unsafe double[][] DotWithTransposed(this decimal[,] a, float[][] b #endif int n = a.Rows(); - fixed (decimal* A = a) - for (int j = 0; j < b.Length; j++) - { - decimal* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < b.Length; j++) + { + decimal* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -96888,7 +98156,7 @@ public static double[][] DotWithTransposed(this decimal[][] a, float[] columnVec #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this decimal[,] a, float[,] b, double[,] result) + public static double[,] TransposeAndDot(this decimal[,] a, float[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -96899,35 +98167,37 @@ public static double[][] DotWithTransposed(this decimal[][] a, float[] columnVec throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - decimal aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + decimal aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -97321,18 +98591,22 @@ public static double[][] DivideByDiagonal(this decimal[][] a, float[] diagonal, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this decimal[] a, float[] b, double[,] result) + public static double[,] Outer(this decimal[] a, float[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -97495,25 +98769,28 @@ public static double[][] Kronecker(this decimal[][] a, float[][] b, double[][] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this decimal[][] a, float[,] b, double[][] result) + public static double[][] Kronecker(this decimal[][] a, float[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -97567,18 +98844,22 @@ public static double[][] Kronecker(this decimal[,] a, float[][] b, double[][] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this decimal[] a, float[] b, double[] result) + public static double[] Kronecker(this decimal[] a, float[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -97772,7 +99053,7 @@ public static int[][] Dot(this decimal[][] a, float[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this decimal[,] a, float[][] b, int[][] result) + public static int[][] Dot(this decimal[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -97786,21 +99067,24 @@ public static unsafe int[][] Dot(this decimal[,] a, float[][] b, int[][] result) var t = new float[K]; - fixed (decimal* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - decimal* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (int)s; - } - } + decimal* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -97878,7 +99162,7 @@ public static int[] Dot(this decimal[][] matrix, float[] columnVector, int[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this decimal[,] matrix, float[] columnVector, int[] result) + public static int[] Dot(this decimal[,] matrix, float[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -97887,55 +99171,54 @@ public static unsafe int[] Dot(this decimal[,] matrix, float[] columnVector, int if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (decimal* a = matrix) + fixed (float* x = columnVector) + fixed (int* r = result) + { + decimal* pa1 = a; + decimal* pa2 = a + cols; + int* pr = r; - fixed (decimal* a = matrix) - fixed (float* x = columnVector) - fixed (int* r = result) - { - decimal* pa1 = a; - decimal* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -98041,7 +99324,7 @@ public static int[] Dot(this decimal[] rowVector, float[][] matrix, int[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this decimal[][] a, float[,] b, int[][] result) + public static int[][] DotWithTransposed(this decimal[][] a, float[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -98051,19 +99334,22 @@ public static unsafe int[][] DotWithTransposed(this decimal[][] a, float[,] b, i #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - decimal[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + decimal[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -98085,7 +99371,7 @@ public static unsafe int[][] DotWithTransposed(this decimal[][] a, float[,] b, i #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this decimal[,] a, float[][] b, int[][] result) + public static int[][] DotWithTransposed(this decimal[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -98095,20 +99381,22 @@ public static unsafe int[][] DotWithTransposed(this decimal[,] a, float[][] b, i #endif int n = a.Rows(); - fixed (decimal* A = a) - for (int j = 0; j < b.Length; j++) - { - decimal* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (decimal* A = a) + for (int j = 0; j < b.Length; j++) + { + decimal* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -98254,7 +99542,7 @@ public static int[][] DotWithTransposed(this decimal[][] a, float[] columnVector #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this decimal[,] a, float[,] b, int[,] result) + public static int[,] TransposeAndDot(this decimal[,] a, float[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -98265,35 +99553,37 @@ public static int[][] DotWithTransposed(this decimal[][] a, float[] columnVector throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - decimal aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + decimal aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -98687,18 +99977,22 @@ public static int[][] DivideByDiagonal(this decimal[][] a, float[] diagonal, int #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this decimal[] a, float[] b, int[,] result) + public static int[,] Outer(this decimal[] a, float[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -98861,25 +100155,28 @@ public static int[][] Kronecker(this decimal[][] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this decimal[][] a, float[,] b, int[][] result) + public static int[][] Kronecker(this decimal[][] a, float[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); + } + } + return result; } @@ -98933,18 +100230,22 @@ public static int[][] Kronecker(this decimal[,] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this decimal[] a, float[] b, int[] result) + public static int[] Kronecker(this decimal[] a, float[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -99138,7 +100439,7 @@ public static byte[][] Dot(this byte[][] a, byte[,] b, byte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] Dot(this byte[,] a, byte[][] b, byte[][] result) + public static byte[][] Dot(this byte[,] a, byte[][] b, byte[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -99152,21 +100453,24 @@ public static unsafe byte[][] Dot(this byte[,] a, byte[][] b, byte[][] result) var t = new byte[K]; - fixed (byte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - byte* pa = A; - for (int i = 0; i < N; i++) - { - byte s = (byte)0; - for (int k = 0; k < t.Length; k++) - s += (byte)((byte)(*pa++) * (byte)t[k]); - result[i][j] = (byte)s; - } - } + byte* pa = A; + for (int i = 0; i < N; i++) + { + byte s = (byte)0; + for (int k = 0; k < t.Length; k++) + s += (byte)((byte)(*pa++) * (byte)t[k]); + result[i][j] = (byte)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -99244,7 +100548,7 @@ public static byte[] Dot(this byte[][] matrix, byte[] columnVector, byte[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[] Dot(this byte[,] matrix, byte[] columnVector, byte[] result) + public static byte[] Dot(this byte[,] matrix, byte[] columnVector, byte[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -99253,55 +100557,54 @@ public static unsafe byte[] Dot(this byte[,] matrix, byte[] columnVector, byte[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (byte* a = matrix) + fixed (byte* x = columnVector) + fixed (byte* r = result) + { + byte* pa1 = a; + byte* pa2 = a + cols; + byte* pr = r; - fixed (byte* a = matrix) - fixed (byte* x = columnVector) - fixed (byte* r = result) - { - byte* pa1 = a; - byte* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - byte sum1 = 0, sum2 = 0; - byte* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (byte)((byte)(*pa1++) * (byte)(*px)); - sum2 += (byte)((byte)(*pa2++) * (byte)(*px)); - px++; - } - - *pr++ = (byte)sum1; - *pr++ = (byte)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + byte sum1 = 0, sum2 = 0; + byte* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (byte)((byte)(*pa1++) * (byte)(*px)); + sum2 += (byte)((byte)(*pa2++) * (byte)(*px)); + px++; + } + + *pr++ = (byte)sum1; + *pr++ = (byte)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - byte sum = 0; - byte* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + byte sum = 0; + byte* px = x; - for (int j = 0; j < cols; j++) - sum += (byte)((byte)(*pa1++) * (byte)(*px++)); + for (int j = 0; j < cols; j++) + sum += (byte)((byte)(*pa1++) * (byte)(*px++)); - *pr = (byte)sum; - } - } + *pr = (byte)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -99407,7 +100710,7 @@ public static byte[] Dot(this byte[] rowVector, byte[][] matrix, byte[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] DotWithTransposed(this byte[][] a, byte[,] b, byte[][] result) + public static byte[][] DotWithTransposed(this byte[][] a, byte[,] b, byte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -99417,19 +100720,22 @@ public static unsafe byte[][] DotWithTransposed(this byte[][] a, byte[,] b, byte #endif int n = b.Rows(); - fixed (byte* B = b) - for (int i = 0; i < a.Length; i++) - { - byte* pb = B; - byte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - byte sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (byte)((byte)arow[k] * (byte)(*pb++)); - result[i][j] = (byte)sum; - } - } + unsafe + { + fixed (byte* B = b) + for (int i = 0; i < a.Length; i++) + { + byte* pb = B; + byte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + byte sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (byte)((byte)arow[k] * (byte)(*pb++)); + result[i][j] = (byte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -99451,7 +100757,7 @@ public static unsafe byte[][] DotWithTransposed(this byte[][] a, byte[,] b, byte #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] DotWithTransposed(this byte[,] a, byte[][] b, byte[][] result) + public static byte[][] DotWithTransposed(this byte[,] a, byte[][] b, byte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -99461,20 +100767,22 @@ public static unsafe byte[][] DotWithTransposed(this byte[,] a, byte[][] b, byte #endif int n = a.Rows(); - fixed (byte* A = a) - for (int j = 0; j < b.Length; j++) - { - byte* pa = A; - for (int i = 0; i < n; i++) - { - byte sum = 0; - byte[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (byte)((byte)(*pa++) * (byte)brow[k]); - result[i][j] = (byte)sum; - } - } - + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < b.Length; j++) + { + byte* pa = A; + for (int i = 0; i < n; i++) + { + byte sum = 0; + byte[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (byte)((byte)(*pa++) * (byte)brow[k]); + result[i][j] = (byte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -99620,7 +100928,7 @@ public static byte[][] DotWithTransposed(this byte[][] a, byte[] columnVector, b #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[,] TransposeAndDot(this byte[,] a, byte[,] b, byte[,] result) + public static byte[,] TransposeAndDot(this byte[,] a, byte[,] b, byte[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -99631,35 +100939,37 @@ public static byte[][] DotWithTransposed(this byte[][] a, byte[] columnVector, b throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (byte* R = result) + fixed (byte* B = b) + fixed (byte* ptemp = new byte[p]) + { + byte* pr = R; - fixed (byte* R = result) - fixed (byte* B = b) - fixed (byte* ptemp = new byte[p]) - { - byte* pr = R; - - for (int i = 0; i < m; i++) - { - byte* pt = ptemp; - byte* pb = B; - - for (int k = 0; k < n; k++) - { - byte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (byte)((byte)aval * (byte)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (byte)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + byte* pt = ptemp; + byte* pb = B; + + for (int k = 0; k < n; k++) + { + byte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (byte)((byte)aval * (byte)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (byte)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -100053,18 +101363,22 @@ public static byte[][] DivideByDiagonal(this byte[][] a, byte[] diagonal, byte[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[,] Outer(this byte[] a, byte[] b, byte[,] result) + public static byte[,] Outer(this byte[] a, byte[] b, byte[,] result) { - fixed (byte* R = result) - { - byte* pr = R; - for (int i = 0; i < a.Length; i++) - { - byte x = (byte)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (byte)(x * (byte)b[j]); - } - } + unsafe + { + fixed (byte* R = result) + { + byte* pr = R; + for (int i = 0; i < a.Length; i++) + { + byte x = (byte)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (byte)(x * (byte)b[j]); + } + } + } + return result; } @@ -100227,25 +101541,28 @@ public static byte[][] Kronecker(this byte[][] a, byte[][] b, byte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] Kronecker(this byte[][] a, byte[,] b, byte[][] result) + public static byte[][] Kronecker(this byte[][] a, byte[,] b, byte[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (byte* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - byte aval = (byte)a[i][j]; - byte* pb = B; + unsafe + { + fixed (byte* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + byte aval = (byte)a[i][j]; + byte* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (byte)(aval * (byte)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (byte)(aval * (byte)(*pb++)); + } + } + return result; } @@ -100299,18 +101616,22 @@ public static byte[][] Kronecker(this byte[,] a, byte[][] b, byte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[] Kronecker(this byte[] a, byte[] b, byte[] result) + public static byte[] Kronecker(this byte[] a, byte[] b, byte[] result) { - fixed (byte* R = result) - { - byte* pr = R; - for (int i = 0; i < a.Length; i++) - { - byte x = (byte)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (byte)(x * (byte)b[j]); - } - } + unsafe + { + fixed (byte* R = result) + { + byte* pr = R; + for (int i = 0; i < a.Length; i++) + { + byte x = (byte)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (byte)(x * (byte)b[j]); + } + } + } + return result; } @@ -100504,7 +101825,7 @@ public static double[][] Dot(this byte[][] a, byte[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this byte[,] a, byte[][] b, double[][] result) + public static double[][] Dot(this byte[,] a, byte[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -100518,21 +101839,24 @@ public static unsafe double[][] Dot(this byte[,] a, byte[][] b, double[][] resul var t = new byte[K]; - fixed (byte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - byte* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + byte* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -100610,7 +101934,7 @@ public static double[] Dot(this byte[][] matrix, byte[] columnVector, double[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this byte[,] matrix, byte[] columnVector, double[] result) + public static double[] Dot(this byte[,] matrix, byte[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -100619,55 +101943,54 @@ public static unsafe double[] Dot(this byte[,] matrix, byte[] columnVector, doub if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (byte* a = matrix) + fixed (byte* x = columnVector) + fixed (double* r = result) + { + byte* pa1 = a; + byte* pa2 = a + cols; + double* pr = r; - fixed (byte* a = matrix) - fixed (byte* x = columnVector) - fixed (double* r = result) - { - byte* pa1 = a; - byte* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - byte* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + byte* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - byte* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + byte* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -100773,7 +102096,7 @@ public static double[] Dot(this byte[] rowVector, byte[][] matrix, double[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this byte[][] a, byte[,] b, double[][] result) + public static double[][] DotWithTransposed(this byte[][] a, byte[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -100783,19 +102106,22 @@ public static unsafe double[][] DotWithTransposed(this byte[][] a, byte[,] b, do #endif int n = b.Rows(); - fixed (byte* B = b) - for (int i = 0; i < a.Length; i++) - { - byte* pb = B; - byte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (byte* B = b) + for (int i = 0; i < a.Length; i++) + { + byte* pb = B; + byte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -100817,7 +102143,7 @@ public static unsafe double[][] DotWithTransposed(this byte[][] a, byte[,] b, do #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this byte[,] a, byte[][] b, double[][] result) + public static double[][] DotWithTransposed(this byte[,] a, byte[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -100827,20 +102153,22 @@ public static unsafe double[][] DotWithTransposed(this byte[,] a, byte[][] b, do #endif int n = a.Rows(); - fixed (byte* A = a) - for (int j = 0; j < b.Length; j++) - { - byte* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - byte[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < b.Length; j++) + { + byte* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + byte[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -100986,7 +102314,7 @@ public static double[][] DotWithTransposed(this byte[][] a, byte[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this byte[,] a, byte[,] b, double[,] result) + public static double[,] TransposeAndDot(this byte[,] a, byte[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -100997,35 +102325,37 @@ public static double[][] DotWithTransposed(this byte[][] a, byte[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (byte* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (byte* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - byte* pb = B; - - for (int k = 0; k < n; k++) - { - byte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + byte* pb = B; + + for (int k = 0; k < n; k++) + { + byte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -101419,18 +102749,22 @@ public static double[][] DivideByDiagonal(this byte[][] a, byte[] diagonal, doub #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this byte[] a, byte[] b, double[,] result) + public static double[,] Outer(this byte[] a, byte[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -101593,25 +102927,28 @@ public static double[][] Kronecker(this byte[][] a, byte[][] b, double[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this byte[][] a, byte[,] b, double[][] result) + public static double[][] Kronecker(this byte[][] a, byte[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (byte* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - byte* pb = B; + unsafe + { + fixed (byte* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + byte* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -101665,18 +103002,22 @@ public static double[][] Kronecker(this byte[,] a, byte[][] b, double[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this byte[] a, byte[] b, double[] result) + public static double[] Kronecker(this byte[] a, byte[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -101870,7 +103211,7 @@ public static int[][] Dot(this byte[][] a, byte[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this byte[,] a, byte[][] b, int[][] result) + public static int[][] Dot(this byte[,] a, byte[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -101884,21 +103225,24 @@ public static unsafe int[][] Dot(this byte[,] a, byte[][] b, int[][] result) var t = new byte[K]; - fixed (byte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - byte* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + byte* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -101976,7 +103320,7 @@ public static int[] Dot(this byte[][] matrix, byte[] columnVector, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this byte[,] matrix, byte[] columnVector, int[] result) + public static int[] Dot(this byte[,] matrix, byte[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -101985,55 +103329,54 @@ public static unsafe int[] Dot(this byte[,] matrix, byte[] columnVector, int[] r if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (byte* a = matrix) + fixed (byte* x = columnVector) + fixed (int* r = result) + { + byte* pa1 = a; + byte* pa2 = a + cols; + int* pr = r; - fixed (byte* a = matrix) - fixed (byte* x = columnVector) - fixed (int* r = result) - { - byte* pa1 = a; - byte* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - byte* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + byte* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - byte* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + byte* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -102139,7 +103482,7 @@ public static int[] Dot(this byte[] rowVector, byte[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this byte[][] a, byte[,] b, int[][] result) + public static int[][] DotWithTransposed(this byte[][] a, byte[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -102149,19 +103492,22 @@ public static unsafe int[][] DotWithTransposed(this byte[][] a, byte[,] b, int[] #endif int n = b.Rows(); - fixed (byte* B = b) - for (int i = 0; i < a.Length; i++) - { - byte* pb = B; - byte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (byte* B = b) + for (int i = 0; i < a.Length; i++) + { + byte* pb = B; + byte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -102183,7 +103529,7 @@ public static unsafe int[][] DotWithTransposed(this byte[][] a, byte[,] b, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this byte[,] a, byte[][] b, int[][] result) + public static int[][] DotWithTransposed(this byte[,] a, byte[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -102193,20 +103539,22 @@ public static unsafe int[][] DotWithTransposed(this byte[,] a, byte[][] b, int[] #endif int n = a.Rows(); - fixed (byte* A = a) - for (int j = 0; j < b.Length; j++) - { - byte* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - byte[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < b.Length; j++) + { + byte* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + byte[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -102352,7 +103700,7 @@ public static int[][] DotWithTransposed(this byte[][] a, byte[] columnVector, in #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this byte[,] a, byte[,] b, int[,] result) + public static int[,] TransposeAndDot(this byte[,] a, byte[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -102363,35 +103711,37 @@ public static int[][] DotWithTransposed(this byte[][] a, byte[] columnVector, in throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (byte* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (byte* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - byte* pb = B; - - for (int k = 0; k < n; k++) - { - byte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + byte* pb = B; + + for (int k = 0; k < n; k++) + { + byte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -102785,18 +104135,22 @@ public static int[][] DivideByDiagonal(this byte[][] a, byte[] diagonal, int[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this byte[] a, byte[] b, int[,] result) + public static int[,] Outer(this byte[] a, byte[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -102959,25 +104313,28 @@ public static int[][] Kronecker(this byte[][] a, byte[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this byte[][] a, byte[,] b, int[][] result) + public static int[][] Kronecker(this byte[][] a, byte[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (byte* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - byte* pb = B; + unsafe + { + fixed (byte* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + byte* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -103031,18 +104388,22 @@ public static int[][] Kronecker(this byte[,] a, byte[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this byte[] a, byte[] b, int[] result) + public static int[] Kronecker(this byte[] a, byte[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -103236,7 +104597,7 @@ public static byte[][] Dot(this byte[][] a, double[,] b, byte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] Dot(this byte[,] a, double[][] b, byte[][] result) + public static byte[][] Dot(this byte[,] a, double[][] b, byte[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -103250,21 +104611,24 @@ public static unsafe byte[][] Dot(this byte[,] a, double[][] b, byte[][] result) var t = new double[K]; - fixed (byte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - byte* pa = A; - for (int i = 0; i < N; i++) - { - byte s = (byte)0; - for (int k = 0; k < t.Length; k++) - s += (byte)((byte)(*pa++) * (byte)t[k]); - result[i][j] = (byte)s; - } - } + byte* pa = A; + for (int i = 0; i < N; i++) + { + byte s = (byte)0; + for (int k = 0; k < t.Length; k++) + s += (byte)((byte)(*pa++) * (byte)t[k]); + result[i][j] = (byte)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -103342,7 +104706,7 @@ public static byte[] Dot(this byte[][] matrix, double[] columnVector, byte[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[] Dot(this byte[,] matrix, double[] columnVector, byte[] result) + public static byte[] Dot(this byte[,] matrix, double[] columnVector, byte[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -103351,55 +104715,54 @@ public static unsafe byte[] Dot(this byte[,] matrix, double[] columnVector, byte if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (byte* a = matrix) + fixed (double* x = columnVector) + fixed (byte* r = result) + { + byte* pa1 = a; + byte* pa2 = a + cols; + byte* pr = r; - fixed (byte* a = matrix) - fixed (double* x = columnVector) - fixed (byte* r = result) - { - byte* pa1 = a; - byte* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - byte sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (byte)((byte)(*pa1++) * (byte)(*px)); - sum2 += (byte)((byte)(*pa2++) * (byte)(*px)); - px++; - } - - *pr++ = (byte)sum1; - *pr++ = (byte)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + byte sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (byte)((byte)(*pa1++) * (byte)(*px)); + sum2 += (byte)((byte)(*pa2++) * (byte)(*px)); + px++; + } + + *pr++ = (byte)sum1; + *pr++ = (byte)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - byte sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + byte sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (byte)((byte)(*pa1++) * (byte)(*px++)); + for (int j = 0; j < cols; j++) + sum += (byte)((byte)(*pa1++) * (byte)(*px++)); - *pr = (byte)sum; - } - } + *pr = (byte)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -103505,7 +104868,7 @@ public static byte[] Dot(this byte[] rowVector, double[][] matrix, byte[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] DotWithTransposed(this byte[][] a, double[,] b, byte[][] result) + public static byte[][] DotWithTransposed(this byte[][] a, double[,] b, byte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -103515,19 +104878,22 @@ public static unsafe byte[][] DotWithTransposed(this byte[][] a, double[,] b, by #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - byte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - byte sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (byte)((byte)arow[k] * (byte)(*pb++)); - result[i][j] = (byte)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + byte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + byte sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (byte)((byte)arow[k] * (byte)(*pb++)); + result[i][j] = (byte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -103549,7 +104915,7 @@ public static unsafe byte[][] DotWithTransposed(this byte[][] a, double[,] b, by #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] DotWithTransposed(this byte[,] a, double[][] b, byte[][] result) + public static byte[][] DotWithTransposed(this byte[,] a, double[][] b, byte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -103559,20 +104925,22 @@ public static unsafe byte[][] DotWithTransposed(this byte[,] a, double[][] b, by #endif int n = a.Rows(); - fixed (byte* A = a) - for (int j = 0; j < b.Length; j++) - { - byte* pa = A; - for (int i = 0; i < n; i++) - { - byte sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (byte)((byte)(*pa++) * (byte)brow[k]); - result[i][j] = (byte)sum; - } - } - + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < b.Length; j++) + { + byte* pa = A; + for (int i = 0; i < n; i++) + { + byte sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (byte)((byte)(*pa++) * (byte)brow[k]); + result[i][j] = (byte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -103718,7 +105086,7 @@ public static byte[][] DotWithTransposed(this byte[][] a, double[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[,] TransposeAndDot(this byte[,] a, double[,] b, byte[,] result) + public static byte[,] TransposeAndDot(this byte[,] a, double[,] b, byte[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -103729,35 +105097,37 @@ public static byte[][] DotWithTransposed(this byte[][] a, double[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (byte* R = result) + fixed (double* B = b) + fixed (byte* ptemp = new byte[p]) + { + byte* pr = R; - fixed (byte* R = result) - fixed (double* B = b) - fixed (byte* ptemp = new byte[p]) - { - byte* pr = R; - - for (int i = 0; i < m; i++) - { - byte* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - byte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (byte)((byte)aval * (byte)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (byte)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + byte* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + byte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (byte)((byte)aval * (byte)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (byte)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -104151,18 +105521,22 @@ public static byte[][] DivideByDiagonal(this byte[][] a, double[] diagonal, byte #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[,] Outer(this byte[] a, double[] b, byte[,] result) + public static byte[,] Outer(this byte[] a, double[] b, byte[,] result) { - fixed (byte* R = result) - { - byte* pr = R; - for (int i = 0; i < a.Length; i++) - { - byte x = (byte)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (byte)(x * (byte)b[j]); - } - } + unsafe + { + fixed (byte* R = result) + { + byte* pr = R; + for (int i = 0; i < a.Length; i++) + { + byte x = (byte)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (byte)(x * (byte)b[j]); + } + } + } + return result; } @@ -104325,25 +105699,28 @@ public static byte[][] Kronecker(this byte[][] a, double[][] b, byte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] Kronecker(this byte[][] a, double[,] b, byte[][] result) + public static byte[][] Kronecker(this byte[][] a, double[,] b, byte[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - byte aval = (byte)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + byte aval = (byte)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (byte)(aval * (byte)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (byte)(aval * (byte)(*pb++)); + } + } + return result; } @@ -104397,18 +105774,22 @@ public static byte[][] Kronecker(this byte[,] a, double[][] b, byte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[] Kronecker(this byte[] a, double[] b, byte[] result) + public static byte[] Kronecker(this byte[] a, double[] b, byte[] result) { - fixed (byte* R = result) - { - byte* pr = R; - for (int i = 0; i < a.Length; i++) - { - byte x = (byte)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (byte)(x * (byte)b[j]); - } - } + unsafe + { + fixed (byte* R = result) + { + byte* pr = R; + for (int i = 0; i < a.Length; i++) + { + byte x = (byte)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (byte)(x * (byte)b[j]); + } + } + } + return result; } @@ -104602,7 +105983,7 @@ public static double[][] Dot(this byte[][] a, double[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this byte[,] a, double[][] b, double[][] result) + public static double[][] Dot(this byte[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -104616,21 +105997,24 @@ public static unsafe double[][] Dot(this byte[,] a, double[][] b, double[][] res var t = new double[K]; - fixed (byte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - byte* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + byte* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -104708,7 +106092,7 @@ public static double[] Dot(this byte[][] matrix, double[] columnVector, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this byte[,] matrix, double[] columnVector, double[] result) + public static double[] Dot(this byte[,] matrix, double[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -104717,55 +106101,54 @@ public static unsafe double[] Dot(this byte[,] matrix, double[] columnVector, do if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (byte* a = matrix) + fixed (double* x = columnVector) + fixed (double* r = result) + { + byte* pa1 = a; + byte* pa2 = a + cols; + double* pr = r; - fixed (byte* a = matrix) - fixed (double* x = columnVector) - fixed (double* r = result) - { - byte* pa1 = a; - byte* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -104871,7 +106254,7 @@ public static double[] Dot(this byte[] rowVector, double[][] matrix, double[] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this byte[][] a, double[,] b, double[][] result) + public static double[][] DotWithTransposed(this byte[][] a, double[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -104881,19 +106264,22 @@ public static unsafe double[][] DotWithTransposed(this byte[][] a, double[,] b, #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - byte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + byte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -104915,7 +106301,7 @@ public static unsafe double[][] DotWithTransposed(this byte[][] a, double[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this byte[,] a, double[][] b, double[][] result) + public static double[][] DotWithTransposed(this byte[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -104925,20 +106311,22 @@ public static unsafe double[][] DotWithTransposed(this byte[,] a, double[][] b, #endif int n = a.Rows(); - fixed (byte* A = a) - for (int j = 0; j < b.Length; j++) - { - byte* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < b.Length; j++) + { + byte* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -105084,7 +106472,7 @@ public static double[][] DotWithTransposed(this byte[][] a, double[] columnVecto #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this byte[,] a, double[,] b, double[,] result) + public static double[,] TransposeAndDot(this byte[,] a, double[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -105095,35 +106483,37 @@ public static double[][] DotWithTransposed(this byte[][] a, double[] columnVecto throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (double* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (double* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - byte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + byte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -105517,18 +106907,22 @@ public static double[][] DivideByDiagonal(this byte[][] a, double[] diagonal, do #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this byte[] a, double[] b, double[,] result) + public static double[,] Outer(this byte[] a, double[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -105691,25 +107085,28 @@ public static double[][] Kronecker(this byte[][] a, double[][] b, double[][] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this byte[][] a, double[,] b, double[][] result) + public static double[][] Kronecker(this byte[][] a, double[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -105763,18 +107160,22 @@ public static double[][] Kronecker(this byte[,] a, double[][] b, double[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this byte[] a, double[] b, double[] result) + public static double[] Kronecker(this byte[] a, double[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -105968,7 +107369,7 @@ public static int[][] Dot(this byte[][] a, double[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this byte[,] a, double[][] b, int[][] result) + public static int[][] Dot(this byte[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -105982,21 +107383,24 @@ public static unsafe int[][] Dot(this byte[,] a, double[][] b, int[][] result) var t = new double[K]; - fixed (byte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - byte* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + byte* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -106074,7 +107478,7 @@ public static int[] Dot(this byte[][] matrix, double[] columnVector, int[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this byte[,] matrix, double[] columnVector, int[] result) + public static int[] Dot(this byte[,] matrix, double[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -106083,55 +107487,54 @@ public static unsafe int[] Dot(this byte[,] matrix, double[] columnVector, int[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (byte* a = matrix) + fixed (double* x = columnVector) + fixed (int* r = result) + { + byte* pa1 = a; + byte* pa2 = a + cols; + int* pr = r; - fixed (byte* a = matrix) - fixed (double* x = columnVector) - fixed (int* r = result) - { - byte* pa1 = a; - byte* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -106237,7 +107640,7 @@ public static int[] Dot(this byte[] rowVector, double[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this byte[][] a, double[,] b, int[][] result) + public static int[][] DotWithTransposed(this byte[][] a, double[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -106247,19 +107650,22 @@ public static unsafe int[][] DotWithTransposed(this byte[][] a, double[,] b, int #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - byte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + byte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -106281,7 +107687,7 @@ public static unsafe int[][] DotWithTransposed(this byte[][] a, double[,] b, int #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this byte[,] a, double[][] b, int[][] result) + public static int[][] DotWithTransposed(this byte[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -106291,20 +107697,22 @@ public static unsafe int[][] DotWithTransposed(this byte[,] a, double[][] b, int #endif int n = a.Rows(); - fixed (byte* A = a) - for (int j = 0; j < b.Length; j++) - { - byte* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < b.Length; j++) + { + byte* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -106450,7 +107858,7 @@ public static int[][] DotWithTransposed(this byte[][] a, double[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this byte[,] a, double[,] b, int[,] result) + public static int[,] TransposeAndDot(this byte[,] a, double[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -106461,35 +107869,37 @@ public static int[][] DotWithTransposed(this byte[][] a, double[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (double* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (double* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - byte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + byte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -106883,18 +108293,22 @@ public static int[][] DivideByDiagonal(this byte[][] a, double[] diagonal, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this byte[] a, double[] b, int[,] result) + public static int[,] Outer(this byte[] a, double[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -107057,25 +108471,28 @@ public static int[][] Kronecker(this byte[][] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this byte[][] a, double[,] b, int[][] result) + public static int[][] Kronecker(this byte[][] a, double[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -107129,18 +108546,22 @@ public static int[][] Kronecker(this byte[,] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this byte[] a, double[] b, int[] result) + public static int[] Kronecker(this byte[] a, double[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -107334,7 +108755,7 @@ public static byte[][] Dot(this byte[][] a, int[,] b, byte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] Dot(this byte[,] a, int[][] b, byte[][] result) + public static byte[][] Dot(this byte[,] a, int[][] b, byte[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -107348,21 +108769,24 @@ public static unsafe byte[][] Dot(this byte[,] a, int[][] b, byte[][] result) var t = new int[K]; - fixed (byte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - byte* pa = A; - for (int i = 0; i < N; i++) - { - byte s = (byte)0; - for (int k = 0; k < t.Length; k++) - s += (byte)((byte)(*pa++) * (byte)t[k]); - result[i][j] = (byte)s; - } - } + byte* pa = A; + for (int i = 0; i < N; i++) + { + byte s = (byte)0; + for (int k = 0; k < t.Length; k++) + s += (byte)((byte)(*pa++) * (byte)t[k]); + result[i][j] = (byte)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -107440,7 +108864,7 @@ public static byte[] Dot(this byte[][] matrix, int[] columnVector, byte[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[] Dot(this byte[,] matrix, int[] columnVector, byte[] result) + public static byte[] Dot(this byte[,] matrix, int[] columnVector, byte[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -107449,55 +108873,54 @@ public static unsafe byte[] Dot(this byte[,] matrix, int[] columnVector, byte[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (byte* a = matrix) + fixed (int* x = columnVector) + fixed (byte* r = result) + { + byte* pa1 = a; + byte* pa2 = a + cols; + byte* pr = r; - fixed (byte* a = matrix) - fixed (int* x = columnVector) - fixed (byte* r = result) - { - byte* pa1 = a; - byte* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - byte sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (byte)((byte)(*pa1++) * (byte)(*px)); - sum2 += (byte)((byte)(*pa2++) * (byte)(*px)); - px++; - } - - *pr++ = (byte)sum1; - *pr++ = (byte)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + byte sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (byte)((byte)(*pa1++) * (byte)(*px)); + sum2 += (byte)((byte)(*pa2++) * (byte)(*px)); + px++; + } + + *pr++ = (byte)sum1; + *pr++ = (byte)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - byte sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + byte sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (byte)((byte)(*pa1++) * (byte)(*px++)); + for (int j = 0; j < cols; j++) + sum += (byte)((byte)(*pa1++) * (byte)(*px++)); - *pr = (byte)sum; - } - } + *pr = (byte)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -107603,7 +109026,7 @@ public static byte[] Dot(this byte[] rowVector, int[][] matrix, byte[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] DotWithTransposed(this byte[][] a, int[,] b, byte[][] result) + public static byte[][] DotWithTransposed(this byte[][] a, int[,] b, byte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -107613,19 +109036,22 @@ public static unsafe byte[][] DotWithTransposed(this byte[][] a, int[,] b, byte[ #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - byte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - byte sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (byte)((byte)arow[k] * (byte)(*pb++)); - result[i][j] = (byte)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + byte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + byte sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (byte)((byte)arow[k] * (byte)(*pb++)); + result[i][j] = (byte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -107647,7 +109073,7 @@ public static unsafe byte[][] DotWithTransposed(this byte[][] a, int[,] b, byte[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] DotWithTransposed(this byte[,] a, int[][] b, byte[][] result) + public static byte[][] DotWithTransposed(this byte[,] a, int[][] b, byte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -107657,20 +109083,22 @@ public static unsafe byte[][] DotWithTransposed(this byte[,] a, int[][] b, byte[ #endif int n = a.Rows(); - fixed (byte* A = a) - for (int j = 0; j < b.Length; j++) - { - byte* pa = A; - for (int i = 0; i < n; i++) - { - byte sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (byte)((byte)(*pa++) * (byte)brow[k]); - result[i][j] = (byte)sum; - } - } - + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < b.Length; j++) + { + byte* pa = A; + for (int i = 0; i < n; i++) + { + byte sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (byte)((byte)(*pa++) * (byte)brow[k]); + result[i][j] = (byte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -107816,7 +109244,7 @@ public static byte[][] DotWithTransposed(this byte[][] a, int[] columnVector, by #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[,] TransposeAndDot(this byte[,] a, int[,] b, byte[,] result) + public static byte[,] TransposeAndDot(this byte[,] a, int[,] b, byte[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -107827,35 +109255,37 @@ public static byte[][] DotWithTransposed(this byte[][] a, int[] columnVector, by throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (byte* R = result) + fixed (int* B = b) + fixed (byte* ptemp = new byte[p]) + { + byte* pr = R; - fixed (byte* R = result) - fixed (int* B = b) - fixed (byte* ptemp = new byte[p]) - { - byte* pr = R; - - for (int i = 0; i < m; i++) - { - byte* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - byte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (byte)((byte)aval * (byte)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (byte)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + byte* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + byte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (byte)((byte)aval * (byte)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (byte)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -108249,18 +109679,22 @@ public static byte[][] DivideByDiagonal(this byte[][] a, int[] diagonal, byte[][ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[,] Outer(this byte[] a, int[] b, byte[,] result) + public static byte[,] Outer(this byte[] a, int[] b, byte[,] result) { - fixed (byte* R = result) - { - byte* pr = R; - for (int i = 0; i < a.Length; i++) - { - byte x = (byte)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (byte)(x * (byte)b[j]); - } - } + unsafe + { + fixed (byte* R = result) + { + byte* pr = R; + for (int i = 0; i < a.Length; i++) + { + byte x = (byte)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (byte)(x * (byte)b[j]); + } + } + } + return result; } @@ -108423,25 +109857,28 @@ public static byte[][] Kronecker(this byte[][] a, int[][] b, byte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] Kronecker(this byte[][] a, int[,] b, byte[][] result) + public static byte[][] Kronecker(this byte[][] a, int[,] b, byte[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - byte aval = (byte)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + byte aval = (byte)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (byte)(aval * (byte)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (byte)(aval * (byte)(*pb++)); + } + } + return result; } @@ -108495,18 +109932,22 @@ public static byte[][] Kronecker(this byte[,] a, int[][] b, byte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[] Kronecker(this byte[] a, int[] b, byte[] result) + public static byte[] Kronecker(this byte[] a, int[] b, byte[] result) { - fixed (byte* R = result) - { - byte* pr = R; - for (int i = 0; i < a.Length; i++) - { - byte x = (byte)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (byte)(x * (byte)b[j]); - } - } + unsafe + { + fixed (byte* R = result) + { + byte* pr = R; + for (int i = 0; i < a.Length; i++) + { + byte x = (byte)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (byte)(x * (byte)b[j]); + } + } + } + return result; } @@ -108700,7 +110141,7 @@ public static int[][] Dot(this byte[][] a, int[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this byte[,] a, int[][] b, int[][] result) + public static int[][] Dot(this byte[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -108714,21 +110155,24 @@ public static unsafe int[][] Dot(this byte[,] a, int[][] b, int[][] result) var t = new int[K]; - fixed (byte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - byte* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + byte* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -108806,7 +110250,7 @@ public static int[] Dot(this byte[][] matrix, int[] columnVector, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this byte[,] matrix, int[] columnVector, int[] result) + public static int[] Dot(this byte[,] matrix, int[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -108815,55 +110259,54 @@ public static unsafe int[] Dot(this byte[,] matrix, int[] columnVector, int[] re if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (byte* a = matrix) + fixed (int* x = columnVector) + fixed (int* r = result) + { + byte* pa1 = a; + byte* pa2 = a + cols; + int* pr = r; - fixed (byte* a = matrix) - fixed (int* x = columnVector) - fixed (int* r = result) - { - byte* pa1 = a; - byte* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -108969,7 +110412,7 @@ public static int[] Dot(this byte[] rowVector, int[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this byte[][] a, int[,] b, int[][] result) + public static int[][] DotWithTransposed(this byte[][] a, int[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -108979,19 +110422,22 @@ public static unsafe int[][] DotWithTransposed(this byte[][] a, int[,] b, int[][ #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - byte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + byte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -109013,7 +110459,7 @@ public static unsafe int[][] DotWithTransposed(this byte[][] a, int[,] b, int[][ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this byte[,] a, int[][] b, int[][] result) + public static int[][] DotWithTransposed(this byte[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -109023,20 +110469,22 @@ public static unsafe int[][] DotWithTransposed(this byte[,] a, int[][] b, int[][ #endif int n = a.Rows(); - fixed (byte* A = a) - for (int j = 0; j < b.Length; j++) - { - byte* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < b.Length; j++) + { + byte* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -109182,7 +110630,7 @@ public static int[][] DotWithTransposed(this byte[][] a, int[] columnVector, int #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this byte[,] a, int[,] b, int[,] result) + public static int[,] TransposeAndDot(this byte[,] a, int[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -109193,35 +110641,37 @@ public static int[][] DotWithTransposed(this byte[][] a, int[] columnVector, int throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (int* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (int* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - byte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + byte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -109615,18 +111065,22 @@ public static int[][] DivideByDiagonal(this byte[][] a, int[] diagonal, int[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this byte[] a, int[] b, int[,] result) + public static int[,] Outer(this byte[] a, int[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -109789,25 +111243,28 @@ public static int[][] Kronecker(this byte[][] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this byte[][] a, int[,] b, int[][] result) + public static int[][] Kronecker(this byte[][] a, int[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -109861,18 +111318,22 @@ public static int[][] Kronecker(this byte[,] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this byte[] a, int[] b, int[] result) + public static int[] Kronecker(this byte[] a, int[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -110066,7 +111527,7 @@ public static double[][] Dot(this byte[][] a, int[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this byte[,] a, int[][] b, double[][] result) + public static double[][] Dot(this byte[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -110080,21 +111541,24 @@ public static unsafe double[][] Dot(this byte[,] a, int[][] b, double[][] result var t = new int[K]; - fixed (byte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - byte* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + byte* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -110172,7 +111636,7 @@ public static double[] Dot(this byte[][] matrix, int[] columnVector, double[] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this byte[,] matrix, int[] columnVector, double[] result) + public static double[] Dot(this byte[,] matrix, int[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -110181,55 +111645,54 @@ public static unsafe double[] Dot(this byte[,] matrix, int[] columnVector, doubl if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (byte* a = matrix) + fixed (int* x = columnVector) + fixed (double* r = result) + { + byte* pa1 = a; + byte* pa2 = a + cols; + double* pr = r; - fixed (byte* a = matrix) - fixed (int* x = columnVector) - fixed (double* r = result) - { - byte* pa1 = a; - byte* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -110335,7 +111798,7 @@ public static double[] Dot(this byte[] rowVector, int[][] matrix, double[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this byte[][] a, int[,] b, double[][] result) + public static double[][] DotWithTransposed(this byte[][] a, int[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -110345,19 +111808,22 @@ public static unsafe double[][] DotWithTransposed(this byte[][] a, int[,] b, dou #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - byte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + byte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -110379,7 +111845,7 @@ public static unsafe double[][] DotWithTransposed(this byte[][] a, int[,] b, dou #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this byte[,] a, int[][] b, double[][] result) + public static double[][] DotWithTransposed(this byte[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -110389,20 +111855,22 @@ public static unsafe double[][] DotWithTransposed(this byte[,] a, int[][] b, dou #endif int n = a.Rows(); - fixed (byte* A = a) - for (int j = 0; j < b.Length; j++) - { - byte* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < b.Length; j++) + { + byte* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -110548,7 +112016,7 @@ public static double[][] DotWithTransposed(this byte[][] a, int[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this byte[,] a, int[,] b, double[,] result) + public static double[,] TransposeAndDot(this byte[,] a, int[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -110559,35 +112027,37 @@ public static double[][] DotWithTransposed(this byte[][] a, int[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (int* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (int* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - byte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + byte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -110981,18 +112451,22 @@ public static double[][] DivideByDiagonal(this byte[][] a, int[] diagonal, doubl #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this byte[] a, int[] b, double[,] result) + public static double[,] Outer(this byte[] a, int[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -111155,25 +112629,28 @@ public static double[][] Kronecker(this byte[][] a, int[][] b, double[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this byte[][] a, int[,] b, double[][] result) + public static double[][] Kronecker(this byte[][] a, int[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -111227,18 +112704,22 @@ public static double[][] Kronecker(this byte[,] a, int[][] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this byte[] a, int[] b, double[] result) + public static double[] Kronecker(this byte[] a, int[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -111432,7 +112913,7 @@ public static byte[][] Dot(this byte[][] a, float[,] b, byte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] Dot(this byte[,] a, float[][] b, byte[][] result) + public static byte[][] Dot(this byte[,] a, float[][] b, byte[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -111446,21 +112927,24 @@ public static unsafe byte[][] Dot(this byte[,] a, float[][] b, byte[][] result) var t = new float[K]; - fixed (byte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - byte* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (byte)s; - } - } + byte* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (byte)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -111538,7 +113022,7 @@ public static byte[] Dot(this byte[][] matrix, float[] columnVector, byte[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[] Dot(this byte[,] matrix, float[] columnVector, byte[] result) + public static byte[] Dot(this byte[,] matrix, float[] columnVector, byte[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -111547,55 +113031,54 @@ public static unsafe byte[] Dot(this byte[,] matrix, float[] columnVector, byte[ if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (byte* a = matrix) + fixed (float* x = columnVector) + fixed (byte* r = result) + { + byte* pa1 = a; + byte* pa2 = a + cols; + byte* pr = r; - fixed (byte* a = matrix) - fixed (float* x = columnVector) - fixed (byte* r = result) - { - byte* pa1 = a; - byte* pa2 = a + cols; - byte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (byte)sum1; - *pr++ = (byte)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (byte)sum1; + *pr++ = (byte)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (byte)sum; - } - } + *pr = (byte)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -111701,7 +113184,7 @@ public static byte[] Dot(this byte[] rowVector, float[][] matrix, byte[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] DotWithTransposed(this byte[][] a, float[,] b, byte[][] result) + public static byte[][] DotWithTransposed(this byte[][] a, float[,] b, byte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -111711,19 +113194,22 @@ public static unsafe byte[][] DotWithTransposed(this byte[][] a, float[,] b, byt #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - byte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (byte)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + byte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (byte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -111745,7 +113231,7 @@ public static unsafe byte[][] DotWithTransposed(this byte[][] a, float[,] b, byt #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] DotWithTransposed(this byte[,] a, float[][] b, byte[][] result) + public static byte[][] DotWithTransposed(this byte[,] a, float[][] b, byte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -111755,20 +113241,22 @@ public static unsafe byte[][] DotWithTransposed(this byte[,] a, float[][] b, byt #endif int n = a.Rows(); - fixed (byte* A = a) - for (int j = 0; j < b.Length; j++) - { - byte* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (byte)sum; - } - } - + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < b.Length; j++) + { + byte* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (byte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -111914,7 +113402,7 @@ public static byte[][] DotWithTransposed(this byte[][] a, float[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[,] TransposeAndDot(this byte[,] a, float[,] b, byte[,] result) + public static byte[,] TransposeAndDot(this byte[,] a, float[,] b, byte[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -111925,35 +113413,37 @@ public static byte[][] DotWithTransposed(this byte[][] a, float[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (byte* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + byte* pr = R; - fixed (byte* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - byte* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - byte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (byte)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + byte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (byte)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -112347,18 +113837,22 @@ public static byte[][] DivideByDiagonal(this byte[][] a, float[] diagonal, byte[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[,] Outer(this byte[] a, float[] b, byte[,] result) + public static byte[,] Outer(this byte[] a, float[] b, byte[,] result) { - fixed (byte* R = result) - { - byte* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (byte)(x * (double)b[j]); - } - } + unsafe + { + fixed (byte* R = result) + { + byte* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (byte)(x * (double)b[j]); + } + } + } + return result; } @@ -112521,25 +114015,28 @@ public static byte[][] Kronecker(this byte[][] a, float[][] b, byte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[][] Kronecker(this byte[][] a, float[,] b, byte[][] result) + public static byte[][] Kronecker(this byte[][] a, float[,] b, byte[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (byte)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (byte)(aval * (double)(*pb++)); + } + } + return result; } @@ -112593,18 +114090,22 @@ public static byte[][] Kronecker(this byte[,] a, float[][] b, byte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe byte[] Kronecker(this byte[] a, float[] b, byte[] result) + public static byte[] Kronecker(this byte[] a, float[] b, byte[] result) { - fixed (byte* R = result) - { - byte* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (byte)(x * (double)b[j]); - } - } + unsafe + { + fixed (byte* R = result) + { + byte* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (byte)(x * (double)b[j]); + } + } + } + return result; } @@ -112798,7 +114299,7 @@ public static float[][] Dot(this byte[][] a, float[,] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Dot(this byte[,] a, float[][] b, float[][] result) + public static float[][] Dot(this byte[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -112812,21 +114313,24 @@ public static unsafe float[][] Dot(this byte[,] a, float[][] b, float[][] result var t = new float[K]; - fixed (byte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - byte* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (float)s; - } - } + byte* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (float)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -112904,7 +114408,7 @@ public static float[] Dot(this byte[][] matrix, float[] columnVector, float[] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Dot(this byte[,] matrix, float[] columnVector, float[] result) + public static float[] Dot(this byte[,] matrix, float[] columnVector, float[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -112913,55 +114417,54 @@ public static unsafe float[] Dot(this byte[,] matrix, float[] columnVector, floa if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (byte* a = matrix) + fixed (float* x = columnVector) + fixed (float* r = result) + { + byte* pa1 = a; + byte* pa2 = a + cols; + float* pr = r; - fixed (byte* a = matrix) - fixed (float* x = columnVector) - fixed (float* r = result) - { - byte* pa1 = a; - byte* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (float)sum1; - *pr++ = (float)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (float)sum1; + *pr++ = (float)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (float)sum; - } - } + *pr = (float)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -113067,7 +114570,7 @@ public static float[] Dot(this byte[] rowVector, float[][] matrix, float[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this byte[][] a, float[,] b, float[][] result) + public static float[][] DotWithTransposed(this byte[][] a, float[,] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -113077,19 +114580,22 @@ public static unsafe float[][] DotWithTransposed(this byte[][] a, float[,] b, fl #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - byte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (float)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + byte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -113111,7 +114617,7 @@ public static unsafe float[][] DotWithTransposed(this byte[][] a, float[,] b, fl #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this byte[,] a, float[][] b, float[][] result) + public static float[][] DotWithTransposed(this byte[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -113121,20 +114627,22 @@ public static unsafe float[][] DotWithTransposed(this byte[,] a, float[][] b, fl #endif int n = a.Rows(); - fixed (byte* A = a) - for (int j = 0; j < b.Length; j++) - { - byte* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (float)sum; - } - } - + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < b.Length; j++) + { + byte* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -113280,7 +114788,7 @@ public static float[][] DotWithTransposed(this byte[][] a, float[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] TransposeAndDot(this byte[,] a, float[,] b, float[,] result) + public static float[,] TransposeAndDot(this byte[,] a, float[,] b, float[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -113291,35 +114799,37 @@ public static float[][] DotWithTransposed(this byte[][] a, float[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (float* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + float* pr = R; - fixed (float* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - float* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - byte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (float)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + byte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (float)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -113713,18 +115223,22 @@ public static float[][] DivideByDiagonal(this byte[][] a, float[] diagonal, floa #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] Outer(this byte[] a, float[] b, float[,] result) + public static float[,] Outer(this byte[] a, float[] b, float[,] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -113887,25 +115401,28 @@ public static float[][] Kronecker(this byte[][] a, float[][] b, float[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Kronecker(this byte[][] a, float[,] b, float[][] result) + public static float[][] Kronecker(this byte[][] a, float[,] b, float[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); + } + } + return result; } @@ -113959,18 +115476,22 @@ public static float[][] Kronecker(this byte[,] a, float[][] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Kronecker(this byte[] a, float[] b, float[] result) + public static float[] Kronecker(this byte[] a, float[] b, float[] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -114164,7 +115685,7 @@ public static double[][] Dot(this byte[][] a, float[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this byte[,] a, float[][] b, double[][] result) + public static double[][] Dot(this byte[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -114178,21 +115699,24 @@ public static unsafe double[][] Dot(this byte[,] a, float[][] b, double[][] resu var t = new float[K]; - fixed (byte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - byte* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + byte* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -114270,7 +115794,7 @@ public static double[] Dot(this byte[][] matrix, float[] columnVector, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this byte[,] matrix, float[] columnVector, double[] result) + public static double[] Dot(this byte[,] matrix, float[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -114279,55 +115803,54 @@ public static unsafe double[] Dot(this byte[,] matrix, float[] columnVector, dou if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (byte* a = matrix) + fixed (float* x = columnVector) + fixed (double* r = result) + { + byte* pa1 = a; + byte* pa2 = a + cols; + double* pr = r; - fixed (byte* a = matrix) - fixed (float* x = columnVector) - fixed (double* r = result) - { - byte* pa1 = a; - byte* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -114433,7 +115956,7 @@ public static double[] Dot(this byte[] rowVector, float[][] matrix, double[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this byte[][] a, float[,] b, double[][] result) + public static double[][] DotWithTransposed(this byte[][] a, float[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -114443,19 +115966,22 @@ public static unsafe double[][] DotWithTransposed(this byte[][] a, float[,] b, d #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - byte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + byte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -114477,7 +116003,7 @@ public static unsafe double[][] DotWithTransposed(this byte[][] a, float[,] b, d #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this byte[,] a, float[][] b, double[][] result) + public static double[][] DotWithTransposed(this byte[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -114487,20 +116013,22 @@ public static unsafe double[][] DotWithTransposed(this byte[,] a, float[][] b, d #endif int n = a.Rows(); - fixed (byte* A = a) - for (int j = 0; j < b.Length; j++) - { - byte* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < b.Length; j++) + { + byte* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -114646,7 +116174,7 @@ public static double[][] DotWithTransposed(this byte[][] a, float[] columnVector #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this byte[,] a, float[,] b, double[,] result) + public static double[,] TransposeAndDot(this byte[,] a, float[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -114657,35 +116185,37 @@ public static double[][] DotWithTransposed(this byte[][] a, float[] columnVector throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - byte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + byte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -115079,18 +116609,22 @@ public static double[][] DivideByDiagonal(this byte[][] a, float[] diagonal, dou #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this byte[] a, float[] b, double[,] result) + public static double[,] Outer(this byte[] a, float[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -115253,25 +116787,28 @@ public static double[][] Kronecker(this byte[][] a, float[][] b, double[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this byte[][] a, float[,] b, double[][] result) + public static double[][] Kronecker(this byte[][] a, float[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -115325,18 +116862,22 @@ public static double[][] Kronecker(this byte[,] a, float[][] b, double[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this byte[] a, float[] b, double[] result) + public static double[] Kronecker(this byte[] a, float[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -115530,7 +117071,7 @@ public static int[][] Dot(this byte[][] a, float[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this byte[,] a, float[][] b, int[][] result) + public static int[][] Dot(this byte[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -115544,21 +117085,24 @@ public static unsafe int[][] Dot(this byte[,] a, float[][] b, int[][] result) var t = new float[K]; - fixed (byte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - byte* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (int)s; - } - } + byte* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -115636,7 +117180,7 @@ public static int[] Dot(this byte[][] matrix, float[] columnVector, int[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this byte[,] matrix, float[] columnVector, int[] result) + public static int[] Dot(this byte[,] matrix, float[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -115645,55 +117189,54 @@ public static unsafe int[] Dot(this byte[,] matrix, float[] columnVector, int[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (byte* a = matrix) + fixed (float* x = columnVector) + fixed (int* r = result) + { + byte* pa1 = a; + byte* pa2 = a + cols; + int* pr = r; - fixed (byte* a = matrix) - fixed (float* x = columnVector) - fixed (int* r = result) - { - byte* pa1 = a; - byte* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -115799,7 +117342,7 @@ public static int[] Dot(this byte[] rowVector, float[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this byte[][] a, float[,] b, int[][] result) + public static int[][] DotWithTransposed(this byte[][] a, float[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -115809,19 +117352,22 @@ public static unsafe int[][] DotWithTransposed(this byte[][] a, float[,] b, int[ #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - byte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + byte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -115843,7 +117389,7 @@ public static unsafe int[][] DotWithTransposed(this byte[][] a, float[,] b, int[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this byte[,] a, float[][] b, int[][] result) + public static int[][] DotWithTransposed(this byte[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -115853,20 +117399,22 @@ public static unsafe int[][] DotWithTransposed(this byte[,] a, float[][] b, int[ #endif int n = a.Rows(); - fixed (byte* A = a) - for (int j = 0; j < b.Length; j++) - { - byte* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (byte* A = a) + for (int j = 0; j < b.Length; j++) + { + byte* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -116012,7 +117560,7 @@ public static int[][] DotWithTransposed(this byte[][] a, float[] columnVector, i #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this byte[,] a, float[,] b, int[,] result) + public static int[,] TransposeAndDot(this byte[,] a, float[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -116023,35 +117571,37 @@ public static int[][] DotWithTransposed(this byte[][] a, float[] columnVector, i throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - byte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + byte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -116445,18 +117995,22 @@ public static int[][] DivideByDiagonal(this byte[][] a, float[] diagonal, int[][ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this byte[] a, float[] b, int[,] result) + public static int[,] Outer(this byte[] a, float[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -116619,25 +118173,28 @@ public static int[][] Kronecker(this byte[][] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this byte[][] a, float[,] b, int[][] result) + public static int[][] Kronecker(this byte[][] a, float[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); + } + } + return result; } @@ -116691,18 +118248,22 @@ public static int[][] Kronecker(this byte[,] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this byte[] a, float[] b, int[] result) + public static int[] Kronecker(this byte[] a, float[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -116896,7 +118457,7 @@ public static short[][] Dot(this short[][] a, short[,] b, short[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] Dot(this short[,] a, short[][] b, short[][] result) + public static short[][] Dot(this short[,] a, short[][] b, short[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -116910,21 +118471,24 @@ public static unsafe short[][] Dot(this short[,] a, short[][] b, short[][] resul var t = new short[K]; - fixed (short* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (short* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - short* pa = A; - for (int i = 0; i < N; i++) - { - short s = (short)0; - for (int k = 0; k < t.Length; k++) - s += (short)((short)(*pa++) * (short)t[k]); - result[i][j] = (short)s; - } - } + short* pa = A; + for (int i = 0; i < N; i++) + { + short s = (short)0; + for (int k = 0; k < t.Length; k++) + s += (short)((short)(*pa++) * (short)t[k]); + result[i][j] = (short)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -117002,7 +118566,7 @@ public static short[] Dot(this short[][] matrix, short[] columnVector, short[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[] Dot(this short[,] matrix, short[] columnVector, short[] result) + public static short[] Dot(this short[,] matrix, short[] columnVector, short[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -117011,55 +118575,54 @@ public static unsafe short[] Dot(this short[,] matrix, short[] columnVector, sho if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (short* a = matrix) + fixed (short* x = columnVector) + fixed (short* r = result) + { + short* pa1 = a; + short* pa2 = a + cols; + short* pr = r; - fixed (short* a = matrix) - fixed (short* x = columnVector) - fixed (short* r = result) - { - short* pa1 = a; - short* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - short sum1 = 0, sum2 = 0; - short* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (short)((short)(*pa1++) * (short)(*px)); - sum2 += (short)((short)(*pa2++) * (short)(*px)); - px++; - } - - *pr++ = (short)sum1; - *pr++ = (short)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + short sum1 = 0, sum2 = 0; + short* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (short)((short)(*pa1++) * (short)(*px)); + sum2 += (short)((short)(*pa2++) * (short)(*px)); + px++; + } + + *pr++ = (short)sum1; + *pr++ = (short)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - short sum = 0; - short* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + short sum = 0; + short* px = x; - for (int j = 0; j < cols; j++) - sum += (short)((short)(*pa1++) * (short)(*px++)); + for (int j = 0; j < cols; j++) + sum += (short)((short)(*pa1++) * (short)(*px++)); - *pr = (short)sum; - } - } + *pr = (short)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -117165,7 +118728,7 @@ public static short[] Dot(this short[] rowVector, short[][] matrix, short[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] DotWithTransposed(this short[][] a, short[,] b, short[][] result) + public static short[][] DotWithTransposed(this short[][] a, short[,] b, short[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -117175,19 +118738,22 @@ public static unsafe short[][] DotWithTransposed(this short[][] a, short[,] b, s #endif int n = b.Rows(); - fixed (short* B = b) - for (int i = 0; i < a.Length; i++) - { - short* pb = B; - short[] arow = a[i]; - for (int j = 0; j < n; j++) - { - short sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (short)((short)arow[k] * (short)(*pb++)); - result[i][j] = (short)sum; - } - } + unsafe + { + fixed (short* B = b) + for (int i = 0; i < a.Length; i++) + { + short* pb = B; + short[] arow = a[i]; + for (int j = 0; j < n; j++) + { + short sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (short)((short)arow[k] * (short)(*pb++)); + result[i][j] = (short)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -117209,7 +118775,7 @@ public static unsafe short[][] DotWithTransposed(this short[][] a, short[,] b, s #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] DotWithTransposed(this short[,] a, short[][] b, short[][] result) + public static short[][] DotWithTransposed(this short[,] a, short[][] b, short[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -117219,20 +118785,22 @@ public static unsafe short[][] DotWithTransposed(this short[,] a, short[][] b, s #endif int n = a.Rows(); - fixed (short* A = a) - for (int j = 0; j < b.Length; j++) - { - short* pa = A; - for (int i = 0; i < n; i++) - { - short sum = 0; - short[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (short)((short)(*pa++) * (short)brow[k]); - result[i][j] = (short)sum; - } - } - + unsafe + { + fixed (short* A = a) + for (int j = 0; j < b.Length; j++) + { + short* pa = A; + for (int i = 0; i < n; i++) + { + short sum = 0; + short[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (short)((short)(*pa++) * (short)brow[k]); + result[i][j] = (short)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -117378,7 +118946,7 @@ public static short[][] DotWithTransposed(this short[][] a, short[] columnVector #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[,] TransposeAndDot(this short[,] a, short[,] b, short[,] result) + public static short[,] TransposeAndDot(this short[,] a, short[,] b, short[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -117389,35 +118957,37 @@ public static short[][] DotWithTransposed(this short[][] a, short[] columnVector throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (short* R = result) + fixed (short* B = b) + fixed (short* ptemp = new short[p]) + { + short* pr = R; - fixed (short* R = result) - fixed (short* B = b) - fixed (short* ptemp = new short[p]) - { - short* pr = R; - - for (int i = 0; i < m; i++) - { - short* pt = ptemp; - short* pb = B; - - for (int k = 0; k < n; k++) - { - short aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (short)((short)aval * (short)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (short)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + short* pt = ptemp; + short* pb = B; + + for (int k = 0; k < n; k++) + { + short aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (short)((short)aval * (short)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (short)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -117811,18 +119381,22 @@ public static short[][] DivideByDiagonal(this short[][] a, short[] diagonal, sho #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[,] Outer(this short[] a, short[] b, short[,] result) + public static short[,] Outer(this short[] a, short[] b, short[,] result) { - fixed (short* R = result) - { - short* pr = R; - for (int i = 0; i < a.Length; i++) - { - short x = (short)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (short)(x * (short)b[j]); - } - } + unsafe + { + fixed (short* R = result) + { + short* pr = R; + for (int i = 0; i < a.Length; i++) + { + short x = (short)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (short)(x * (short)b[j]); + } + } + } + return result; } @@ -117985,25 +119559,28 @@ public static short[][] Kronecker(this short[][] a, short[][] b, short[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] Kronecker(this short[][] a, short[,] b, short[][] result) + public static short[][] Kronecker(this short[][] a, short[,] b, short[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (short* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - short aval = (short)a[i][j]; - short* pb = B; + unsafe + { + fixed (short* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + short aval = (short)a[i][j]; + short* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (short)(aval * (short)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (short)(aval * (short)(*pb++)); + } + } + return result; } @@ -118057,18 +119634,22 @@ public static short[][] Kronecker(this short[,] a, short[][] b, short[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[] Kronecker(this short[] a, short[] b, short[] result) + public static short[] Kronecker(this short[] a, short[] b, short[] result) { - fixed (short* R = result) - { - short* pr = R; - for (int i = 0; i < a.Length; i++) - { - short x = (short)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (short)(x * (short)b[j]); - } - } + unsafe + { + fixed (short* R = result) + { + short* pr = R; + for (int i = 0; i < a.Length; i++) + { + short x = (short)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (short)(x * (short)b[j]); + } + } + } + return result; } @@ -118262,7 +119843,7 @@ public static double[][] Dot(this short[][] a, short[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this short[,] a, short[][] b, double[][] result) + public static double[][] Dot(this short[,] a, short[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -118276,21 +119857,24 @@ public static unsafe double[][] Dot(this short[,] a, short[][] b, double[][] res var t = new short[K]; - fixed (short* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (short* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - short* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + short* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -118368,7 +119952,7 @@ public static double[] Dot(this short[][] matrix, short[] columnVector, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this short[,] matrix, short[] columnVector, double[] result) + public static double[] Dot(this short[,] matrix, short[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -118377,55 +119961,54 @@ public static unsafe double[] Dot(this short[,] matrix, short[] columnVector, do if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (short* a = matrix) + fixed (short* x = columnVector) + fixed (double* r = result) + { + short* pa1 = a; + short* pa2 = a + cols; + double* pr = r; - fixed (short* a = matrix) - fixed (short* x = columnVector) - fixed (double* r = result) - { - short* pa1 = a; - short* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - short* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + short* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - short* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + short* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -118531,7 +120114,7 @@ public static double[] Dot(this short[] rowVector, short[][] matrix, double[] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this short[][] a, short[,] b, double[][] result) + public static double[][] DotWithTransposed(this short[][] a, short[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -118541,19 +120124,22 @@ public static unsafe double[][] DotWithTransposed(this short[][] a, short[,] b, #endif int n = b.Rows(); - fixed (short* B = b) - for (int i = 0; i < a.Length; i++) - { - short* pb = B; - short[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (short* B = b) + for (int i = 0; i < a.Length; i++) + { + short* pb = B; + short[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -118575,7 +120161,7 @@ public static unsafe double[][] DotWithTransposed(this short[][] a, short[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this short[,] a, short[][] b, double[][] result) + public static double[][] DotWithTransposed(this short[,] a, short[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -118585,20 +120171,22 @@ public static unsafe double[][] DotWithTransposed(this short[,] a, short[][] b, #endif int n = a.Rows(); - fixed (short* A = a) - for (int j = 0; j < b.Length; j++) - { - short* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - short[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (short* A = a) + for (int j = 0; j < b.Length; j++) + { + short* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + short[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -118744,7 +120332,7 @@ public static double[][] DotWithTransposed(this short[][] a, short[] columnVecto #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this short[,] a, short[,] b, double[,] result) + public static double[,] TransposeAndDot(this short[,] a, short[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -118755,35 +120343,37 @@ public static double[][] DotWithTransposed(this short[][] a, short[] columnVecto throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (short* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (short* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - short* pb = B; - - for (int k = 0; k < n; k++) - { - short aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + short* pb = B; + + for (int k = 0; k < n; k++) + { + short aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -119177,18 +120767,22 @@ public static double[][] DivideByDiagonal(this short[][] a, short[] diagonal, do #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this short[] a, short[] b, double[,] result) + public static double[,] Outer(this short[] a, short[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -119351,25 +120945,28 @@ public static double[][] Kronecker(this short[][] a, short[][] b, double[][] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this short[][] a, short[,] b, double[][] result) + public static double[][] Kronecker(this short[][] a, short[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (short* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - short* pb = B; + unsafe + { + fixed (short* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + short* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -119423,18 +121020,22 @@ public static double[][] Kronecker(this short[,] a, short[][] b, double[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this short[] a, short[] b, double[] result) + public static double[] Kronecker(this short[] a, short[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -119628,7 +121229,7 @@ public static int[][] Dot(this short[][] a, short[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this short[,] a, short[][] b, int[][] result) + public static int[][] Dot(this short[,] a, short[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -119642,21 +121243,24 @@ public static unsafe int[][] Dot(this short[,] a, short[][] b, int[][] result) var t = new short[K]; - fixed (short* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (short* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - short* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + short* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -119734,7 +121338,7 @@ public static int[] Dot(this short[][] matrix, short[] columnVector, int[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this short[,] matrix, short[] columnVector, int[] result) + public static int[] Dot(this short[,] matrix, short[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -119743,55 +121347,54 @@ public static unsafe int[] Dot(this short[,] matrix, short[] columnVector, int[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (short* a = matrix) + fixed (short* x = columnVector) + fixed (int* r = result) + { + short* pa1 = a; + short* pa2 = a + cols; + int* pr = r; - fixed (short* a = matrix) - fixed (short* x = columnVector) - fixed (int* r = result) - { - short* pa1 = a; - short* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - short* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + short* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - short* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + short* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -119897,7 +121500,7 @@ public static int[] Dot(this short[] rowVector, short[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this short[][] a, short[,] b, int[][] result) + public static int[][] DotWithTransposed(this short[][] a, short[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -119907,19 +121510,22 @@ public static unsafe int[][] DotWithTransposed(this short[][] a, short[,] b, int #endif int n = b.Rows(); - fixed (short* B = b) - for (int i = 0; i < a.Length; i++) - { - short* pb = B; - short[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (short* B = b) + for (int i = 0; i < a.Length; i++) + { + short* pb = B; + short[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -119941,7 +121547,7 @@ public static unsafe int[][] DotWithTransposed(this short[][] a, short[,] b, int #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this short[,] a, short[][] b, int[][] result) + public static int[][] DotWithTransposed(this short[,] a, short[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -119951,20 +121557,22 @@ public static unsafe int[][] DotWithTransposed(this short[,] a, short[][] b, int #endif int n = a.Rows(); - fixed (short* A = a) - for (int j = 0; j < b.Length; j++) - { - short* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - short[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (short* A = a) + for (int j = 0; j < b.Length; j++) + { + short* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + short[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -120110,7 +121718,7 @@ public static int[][] DotWithTransposed(this short[][] a, short[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this short[,] a, short[,] b, int[,] result) + public static int[,] TransposeAndDot(this short[,] a, short[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -120121,35 +121729,37 @@ public static int[][] DotWithTransposed(this short[][] a, short[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (short* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (short* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - short* pb = B; - - for (int k = 0; k < n; k++) - { - short aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + short* pb = B; + + for (int k = 0; k < n; k++) + { + short aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -120543,18 +122153,22 @@ public static int[][] DivideByDiagonal(this short[][] a, short[] diagonal, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this short[] a, short[] b, int[,] result) + public static int[,] Outer(this short[] a, short[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -120717,25 +122331,28 @@ public static int[][] Kronecker(this short[][] a, short[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this short[][] a, short[,] b, int[][] result) + public static int[][] Kronecker(this short[][] a, short[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (short* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - short* pb = B; + unsafe + { + fixed (short* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + short* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -120789,18 +122406,22 @@ public static int[][] Kronecker(this short[,] a, short[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this short[] a, short[] b, int[] result) + public static int[] Kronecker(this short[] a, short[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -120994,7 +122615,7 @@ public static short[][] Dot(this short[][] a, double[,] b, short[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] Dot(this short[,] a, double[][] b, short[][] result) + public static short[][] Dot(this short[,] a, double[][] b, short[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -121008,21 +122629,24 @@ public static unsafe short[][] Dot(this short[,] a, double[][] b, short[][] resu var t = new double[K]; - fixed (short* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (short* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - short* pa = A; - for (int i = 0; i < N; i++) - { - short s = (short)0; - for (int k = 0; k < t.Length; k++) - s += (short)((short)(*pa++) * (short)t[k]); - result[i][j] = (short)s; - } - } + short* pa = A; + for (int i = 0; i < N; i++) + { + short s = (short)0; + for (int k = 0; k < t.Length; k++) + s += (short)((short)(*pa++) * (short)t[k]); + result[i][j] = (short)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -121100,7 +122724,7 @@ public static short[] Dot(this short[][] matrix, double[] columnVector, short[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[] Dot(this short[,] matrix, double[] columnVector, short[] result) + public static short[] Dot(this short[,] matrix, double[] columnVector, short[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -121109,55 +122733,54 @@ public static unsafe short[] Dot(this short[,] matrix, double[] columnVector, sh if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (short* a = matrix) + fixed (double* x = columnVector) + fixed (short* r = result) + { + short* pa1 = a; + short* pa2 = a + cols; + short* pr = r; - fixed (short* a = matrix) - fixed (double* x = columnVector) - fixed (short* r = result) - { - short* pa1 = a; - short* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - short sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (short)((short)(*pa1++) * (short)(*px)); - sum2 += (short)((short)(*pa2++) * (short)(*px)); - px++; - } - - *pr++ = (short)sum1; - *pr++ = (short)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + short sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (short)((short)(*pa1++) * (short)(*px)); + sum2 += (short)((short)(*pa2++) * (short)(*px)); + px++; + } + + *pr++ = (short)sum1; + *pr++ = (short)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - short sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + short sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (short)((short)(*pa1++) * (short)(*px++)); + for (int j = 0; j < cols; j++) + sum += (short)((short)(*pa1++) * (short)(*px++)); - *pr = (short)sum; - } - } + *pr = (short)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -121263,7 +122886,7 @@ public static short[] Dot(this short[] rowVector, double[][] matrix, short[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] DotWithTransposed(this short[][] a, double[,] b, short[][] result) + public static short[][] DotWithTransposed(this short[][] a, double[,] b, short[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -121273,19 +122896,22 @@ public static unsafe short[][] DotWithTransposed(this short[][] a, double[,] b, #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - short[] arow = a[i]; - for (int j = 0; j < n; j++) - { - short sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (short)((short)arow[k] * (short)(*pb++)); - result[i][j] = (short)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + short[] arow = a[i]; + for (int j = 0; j < n; j++) + { + short sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (short)((short)arow[k] * (short)(*pb++)); + result[i][j] = (short)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -121307,7 +122933,7 @@ public static unsafe short[][] DotWithTransposed(this short[][] a, double[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] DotWithTransposed(this short[,] a, double[][] b, short[][] result) + public static short[][] DotWithTransposed(this short[,] a, double[][] b, short[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -121317,20 +122943,22 @@ public static unsafe short[][] DotWithTransposed(this short[,] a, double[][] b, #endif int n = a.Rows(); - fixed (short* A = a) - for (int j = 0; j < b.Length; j++) - { - short* pa = A; - for (int i = 0; i < n; i++) - { - short sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (short)((short)(*pa++) * (short)brow[k]); - result[i][j] = (short)sum; - } - } - + unsafe + { + fixed (short* A = a) + for (int j = 0; j < b.Length; j++) + { + short* pa = A; + for (int i = 0; i < n; i++) + { + short sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (short)((short)(*pa++) * (short)brow[k]); + result[i][j] = (short)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -121476,7 +123104,7 @@ public static short[][] DotWithTransposed(this short[][] a, double[] columnVecto #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[,] TransposeAndDot(this short[,] a, double[,] b, short[,] result) + public static short[,] TransposeAndDot(this short[,] a, double[,] b, short[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -121487,35 +123115,37 @@ public static short[][] DotWithTransposed(this short[][] a, double[] columnVecto throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (short* R = result) + fixed (double* B = b) + fixed (short* ptemp = new short[p]) + { + short* pr = R; - fixed (short* R = result) - fixed (double* B = b) - fixed (short* ptemp = new short[p]) - { - short* pr = R; - - for (int i = 0; i < m; i++) - { - short* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - short aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (short)((short)aval * (short)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (short)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + short* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + short aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (short)((short)aval * (short)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (short)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -121909,18 +123539,22 @@ public static short[][] DivideByDiagonal(this short[][] a, double[] diagonal, sh #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[,] Outer(this short[] a, double[] b, short[,] result) + public static short[,] Outer(this short[] a, double[] b, short[,] result) { - fixed (short* R = result) - { - short* pr = R; - for (int i = 0; i < a.Length; i++) - { - short x = (short)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (short)(x * (short)b[j]); - } - } + unsafe + { + fixed (short* R = result) + { + short* pr = R; + for (int i = 0; i < a.Length; i++) + { + short x = (short)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (short)(x * (short)b[j]); + } + } + } + return result; } @@ -122083,25 +123717,28 @@ public static short[][] Kronecker(this short[][] a, double[][] b, short[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] Kronecker(this short[][] a, double[,] b, short[][] result) + public static short[][] Kronecker(this short[][] a, double[,] b, short[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - short aval = (short)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + short aval = (short)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (short)(aval * (short)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (short)(aval * (short)(*pb++)); + } + } + return result; } @@ -122155,18 +123792,22 @@ public static short[][] Kronecker(this short[,] a, double[][] b, short[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[] Kronecker(this short[] a, double[] b, short[] result) + public static short[] Kronecker(this short[] a, double[] b, short[] result) { - fixed (short* R = result) - { - short* pr = R; - for (int i = 0; i < a.Length; i++) - { - short x = (short)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (short)(x * (short)b[j]); - } - } + unsafe + { + fixed (short* R = result) + { + short* pr = R; + for (int i = 0; i < a.Length; i++) + { + short x = (short)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (short)(x * (short)b[j]); + } + } + } + return result; } @@ -122360,7 +124001,7 @@ public static double[][] Dot(this short[][] a, double[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this short[,] a, double[][] b, double[][] result) + public static double[][] Dot(this short[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -122374,21 +124015,24 @@ public static unsafe double[][] Dot(this short[,] a, double[][] b, double[][] re var t = new double[K]; - fixed (short* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (short* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - short* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + short* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -122466,7 +124110,7 @@ public static double[] Dot(this short[][] matrix, double[] columnVector, double[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this short[,] matrix, double[] columnVector, double[] result) + public static double[] Dot(this short[,] matrix, double[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -122475,55 +124119,54 @@ public static unsafe double[] Dot(this short[,] matrix, double[] columnVector, d if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (short* a = matrix) + fixed (double* x = columnVector) + fixed (double* r = result) + { + short* pa1 = a; + short* pa2 = a + cols; + double* pr = r; - fixed (short* a = matrix) - fixed (double* x = columnVector) - fixed (double* r = result) - { - short* pa1 = a; - short* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -122629,7 +124272,7 @@ public static double[] Dot(this short[] rowVector, double[][] matrix, double[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this short[][] a, double[,] b, double[][] result) + public static double[][] DotWithTransposed(this short[][] a, double[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -122639,19 +124282,22 @@ public static unsafe double[][] DotWithTransposed(this short[][] a, double[,] b, #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - short[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + short[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -122673,7 +124319,7 @@ public static unsafe double[][] DotWithTransposed(this short[][] a, double[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this short[,] a, double[][] b, double[][] result) + public static double[][] DotWithTransposed(this short[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -122683,20 +124329,22 @@ public static unsafe double[][] DotWithTransposed(this short[,] a, double[][] b, #endif int n = a.Rows(); - fixed (short* A = a) - for (int j = 0; j < b.Length; j++) - { - short* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (short* A = a) + for (int j = 0; j < b.Length; j++) + { + short* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -122842,7 +124490,7 @@ public static double[][] DotWithTransposed(this short[][] a, double[] columnVect #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this short[,] a, double[,] b, double[,] result) + public static double[,] TransposeAndDot(this short[,] a, double[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -122853,35 +124501,37 @@ public static double[][] DotWithTransposed(this short[][] a, double[] columnVect throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (double* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (double* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - short aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + short aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -123275,18 +124925,22 @@ public static double[][] DivideByDiagonal(this short[][] a, double[] diagonal, d #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this short[] a, double[] b, double[,] result) + public static double[,] Outer(this short[] a, double[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -123449,25 +125103,28 @@ public static double[][] Kronecker(this short[][] a, double[][] b, double[][] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this short[][] a, double[,] b, double[][] result) + public static double[][] Kronecker(this short[][] a, double[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -123521,18 +125178,22 @@ public static double[][] Kronecker(this short[,] a, double[][] b, double[][] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this short[] a, double[] b, double[] result) + public static double[] Kronecker(this short[] a, double[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -123726,7 +125387,7 @@ public static int[][] Dot(this short[][] a, double[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this short[,] a, double[][] b, int[][] result) + public static int[][] Dot(this short[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -123740,21 +125401,24 @@ public static unsafe int[][] Dot(this short[,] a, double[][] b, int[][] result) var t = new double[K]; - fixed (short* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (short* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - short* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + short* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -123832,7 +125496,7 @@ public static int[] Dot(this short[][] matrix, double[] columnVector, int[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this short[,] matrix, double[] columnVector, int[] result) + public static int[] Dot(this short[,] matrix, double[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -123841,55 +125505,54 @@ public static unsafe int[] Dot(this short[,] matrix, double[] columnVector, int[ if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (short* a = matrix) + fixed (double* x = columnVector) + fixed (int* r = result) + { + short* pa1 = a; + short* pa2 = a + cols; + int* pr = r; - fixed (short* a = matrix) - fixed (double* x = columnVector) - fixed (int* r = result) - { - short* pa1 = a; - short* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -123995,7 +125658,7 @@ public static int[] Dot(this short[] rowVector, double[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this short[][] a, double[,] b, int[][] result) + public static int[][] DotWithTransposed(this short[][] a, double[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -124005,19 +125668,22 @@ public static unsafe int[][] DotWithTransposed(this short[][] a, double[,] b, in #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - short[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + short[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -124039,7 +125705,7 @@ public static unsafe int[][] DotWithTransposed(this short[][] a, double[,] b, in #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this short[,] a, double[][] b, int[][] result) + public static int[][] DotWithTransposed(this short[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -124049,20 +125715,22 @@ public static unsafe int[][] DotWithTransposed(this short[,] a, double[][] b, in #endif int n = a.Rows(); - fixed (short* A = a) - for (int j = 0; j < b.Length; j++) - { - short* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (short* A = a) + for (int j = 0; j < b.Length; j++) + { + short* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -124208,7 +125876,7 @@ public static int[][] DotWithTransposed(this short[][] a, double[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this short[,] a, double[,] b, int[,] result) + public static int[,] TransposeAndDot(this short[,] a, double[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -124219,35 +125887,37 @@ public static int[][] DotWithTransposed(this short[][] a, double[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (double* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (double* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - short aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + short aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -124641,18 +126311,22 @@ public static int[][] DivideByDiagonal(this short[][] a, double[] diagonal, int[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this short[] a, double[] b, int[,] result) + public static int[,] Outer(this short[] a, double[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -124815,25 +126489,28 @@ public static int[][] Kronecker(this short[][] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this short[][] a, double[,] b, int[][] result) + public static int[][] Kronecker(this short[][] a, double[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -124887,18 +126564,22 @@ public static int[][] Kronecker(this short[,] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this short[] a, double[] b, int[] result) + public static int[] Kronecker(this short[] a, double[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -125092,7 +126773,7 @@ public static short[][] Dot(this short[][] a, int[,] b, short[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] Dot(this short[,] a, int[][] b, short[][] result) + public static short[][] Dot(this short[,] a, int[][] b, short[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -125106,21 +126787,24 @@ public static unsafe short[][] Dot(this short[,] a, int[][] b, short[][] result) var t = new int[K]; - fixed (short* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (short* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - short* pa = A; - for (int i = 0; i < N; i++) - { - short s = (short)0; - for (int k = 0; k < t.Length; k++) - s += (short)((short)(*pa++) * (short)t[k]); - result[i][j] = (short)s; - } - } + short* pa = A; + for (int i = 0; i < N; i++) + { + short s = (short)0; + for (int k = 0; k < t.Length; k++) + s += (short)((short)(*pa++) * (short)t[k]); + result[i][j] = (short)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -125198,7 +126882,7 @@ public static short[] Dot(this short[][] matrix, int[] columnVector, short[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[] Dot(this short[,] matrix, int[] columnVector, short[] result) + public static short[] Dot(this short[,] matrix, int[] columnVector, short[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -125207,55 +126891,54 @@ public static unsafe short[] Dot(this short[,] matrix, int[] columnVector, short if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (short* a = matrix) + fixed (int* x = columnVector) + fixed (short* r = result) + { + short* pa1 = a; + short* pa2 = a + cols; + short* pr = r; - fixed (short* a = matrix) - fixed (int* x = columnVector) - fixed (short* r = result) - { - short* pa1 = a; - short* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - short sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (short)((short)(*pa1++) * (short)(*px)); - sum2 += (short)((short)(*pa2++) * (short)(*px)); - px++; - } - - *pr++ = (short)sum1; - *pr++ = (short)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + short sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (short)((short)(*pa1++) * (short)(*px)); + sum2 += (short)((short)(*pa2++) * (short)(*px)); + px++; + } + + *pr++ = (short)sum1; + *pr++ = (short)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - short sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + short sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (short)((short)(*pa1++) * (short)(*px++)); + for (int j = 0; j < cols; j++) + sum += (short)((short)(*pa1++) * (short)(*px++)); - *pr = (short)sum; - } - } + *pr = (short)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -125361,7 +127044,7 @@ public static short[] Dot(this short[] rowVector, int[][] matrix, short[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] DotWithTransposed(this short[][] a, int[,] b, short[][] result) + public static short[][] DotWithTransposed(this short[][] a, int[,] b, short[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -125371,19 +127054,22 @@ public static unsafe short[][] DotWithTransposed(this short[][] a, int[,] b, sho #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - short[] arow = a[i]; - for (int j = 0; j < n; j++) - { - short sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (short)((short)arow[k] * (short)(*pb++)); - result[i][j] = (short)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + short[] arow = a[i]; + for (int j = 0; j < n; j++) + { + short sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (short)((short)arow[k] * (short)(*pb++)); + result[i][j] = (short)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -125405,7 +127091,7 @@ public static unsafe short[][] DotWithTransposed(this short[][] a, int[,] b, sho #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] DotWithTransposed(this short[,] a, int[][] b, short[][] result) + public static short[][] DotWithTransposed(this short[,] a, int[][] b, short[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -125415,20 +127101,22 @@ public static unsafe short[][] DotWithTransposed(this short[,] a, int[][] b, sho #endif int n = a.Rows(); - fixed (short* A = a) - for (int j = 0; j < b.Length; j++) - { - short* pa = A; - for (int i = 0; i < n; i++) - { - short sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (short)((short)(*pa++) * (short)brow[k]); - result[i][j] = (short)sum; - } - } - + unsafe + { + fixed (short* A = a) + for (int j = 0; j < b.Length; j++) + { + short* pa = A; + for (int i = 0; i < n; i++) + { + short sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (short)((short)(*pa++) * (short)brow[k]); + result[i][j] = (short)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -125574,7 +127262,7 @@ public static short[][] DotWithTransposed(this short[][] a, int[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[,] TransposeAndDot(this short[,] a, int[,] b, short[,] result) + public static short[,] TransposeAndDot(this short[,] a, int[,] b, short[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -125585,35 +127273,37 @@ public static short[][] DotWithTransposed(this short[][] a, int[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (short* R = result) + fixed (int* B = b) + fixed (short* ptemp = new short[p]) + { + short* pr = R; - fixed (short* R = result) - fixed (int* B = b) - fixed (short* ptemp = new short[p]) - { - short* pr = R; - - for (int i = 0; i < m; i++) - { - short* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - short aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (short)((short)aval * (short)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (short)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + short* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + short aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (short)((short)aval * (short)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (short)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -126007,18 +127697,22 @@ public static short[][] DivideByDiagonal(this short[][] a, int[] diagonal, short #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[,] Outer(this short[] a, int[] b, short[,] result) + public static short[,] Outer(this short[] a, int[] b, short[,] result) { - fixed (short* R = result) - { - short* pr = R; - for (int i = 0; i < a.Length; i++) - { - short x = (short)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (short)(x * (short)b[j]); - } - } + unsafe + { + fixed (short* R = result) + { + short* pr = R; + for (int i = 0; i < a.Length; i++) + { + short x = (short)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (short)(x * (short)b[j]); + } + } + } + return result; } @@ -126181,25 +127875,28 @@ public static short[][] Kronecker(this short[][] a, int[][] b, short[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] Kronecker(this short[][] a, int[,] b, short[][] result) + public static short[][] Kronecker(this short[][] a, int[,] b, short[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - short aval = (short)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + short aval = (short)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (short)(aval * (short)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (short)(aval * (short)(*pb++)); + } + } + return result; } @@ -126253,18 +127950,22 @@ public static short[][] Kronecker(this short[,] a, int[][] b, short[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[] Kronecker(this short[] a, int[] b, short[] result) + public static short[] Kronecker(this short[] a, int[] b, short[] result) { - fixed (short* R = result) - { - short* pr = R; - for (int i = 0; i < a.Length; i++) - { - short x = (short)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (short)(x * (short)b[j]); - } - } + unsafe + { + fixed (short* R = result) + { + short* pr = R; + for (int i = 0; i < a.Length; i++) + { + short x = (short)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (short)(x * (short)b[j]); + } + } + } + return result; } @@ -126458,7 +128159,7 @@ public static int[][] Dot(this short[][] a, int[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this short[,] a, int[][] b, int[][] result) + public static int[][] Dot(this short[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -126472,21 +128173,24 @@ public static unsafe int[][] Dot(this short[,] a, int[][] b, int[][] result) var t = new int[K]; - fixed (short* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (short* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - short* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + short* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -126564,7 +128268,7 @@ public static int[] Dot(this short[][] matrix, int[] columnVector, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this short[,] matrix, int[] columnVector, int[] result) + public static int[] Dot(this short[,] matrix, int[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -126573,55 +128277,54 @@ public static unsafe int[] Dot(this short[,] matrix, int[] columnVector, int[] r if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (short* a = matrix) + fixed (int* x = columnVector) + fixed (int* r = result) + { + short* pa1 = a; + short* pa2 = a + cols; + int* pr = r; - fixed (short* a = matrix) - fixed (int* x = columnVector) - fixed (int* r = result) - { - short* pa1 = a; - short* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -126727,7 +128430,7 @@ public static int[] Dot(this short[] rowVector, int[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this short[][] a, int[,] b, int[][] result) + public static int[][] DotWithTransposed(this short[][] a, int[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -126737,19 +128440,22 @@ public static unsafe int[][] DotWithTransposed(this short[][] a, int[,] b, int[] #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - short[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + short[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -126771,7 +128477,7 @@ public static unsafe int[][] DotWithTransposed(this short[][] a, int[,] b, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this short[,] a, int[][] b, int[][] result) + public static int[][] DotWithTransposed(this short[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -126781,20 +128487,22 @@ public static unsafe int[][] DotWithTransposed(this short[,] a, int[][] b, int[] #endif int n = a.Rows(); - fixed (short* A = a) - for (int j = 0; j < b.Length; j++) - { - short* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (short* A = a) + for (int j = 0; j < b.Length; j++) + { + short* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -126940,7 +128648,7 @@ public static int[][] DotWithTransposed(this short[][] a, int[] columnVector, in #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this short[,] a, int[,] b, int[,] result) + public static int[,] TransposeAndDot(this short[,] a, int[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -126951,35 +128659,37 @@ public static int[][] DotWithTransposed(this short[][] a, int[] columnVector, in throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (int* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (int* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - short aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + short aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -127373,18 +129083,22 @@ public static int[][] DivideByDiagonal(this short[][] a, int[] diagonal, int[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this short[] a, int[] b, int[,] result) + public static int[,] Outer(this short[] a, int[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -127547,25 +129261,28 @@ public static int[][] Kronecker(this short[][] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this short[][] a, int[,] b, int[][] result) + public static int[][] Kronecker(this short[][] a, int[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -127619,18 +129336,22 @@ public static int[][] Kronecker(this short[,] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this short[] a, int[] b, int[] result) + public static int[] Kronecker(this short[] a, int[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -127824,7 +129545,7 @@ public static double[][] Dot(this short[][] a, int[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this short[,] a, int[][] b, double[][] result) + public static double[][] Dot(this short[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -127838,21 +129559,24 @@ public static unsafe double[][] Dot(this short[,] a, int[][] b, double[][] resul var t = new int[K]; - fixed (short* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (short* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - short* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + short* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -127930,7 +129654,7 @@ public static double[] Dot(this short[][] matrix, int[] columnVector, double[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this short[,] matrix, int[] columnVector, double[] result) + public static double[] Dot(this short[,] matrix, int[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -127939,55 +129663,54 @@ public static unsafe double[] Dot(this short[,] matrix, int[] columnVector, doub if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (short* a = matrix) + fixed (int* x = columnVector) + fixed (double* r = result) + { + short* pa1 = a; + short* pa2 = a + cols; + double* pr = r; - fixed (short* a = matrix) - fixed (int* x = columnVector) - fixed (double* r = result) - { - short* pa1 = a; - short* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -128093,7 +129816,7 @@ public static double[] Dot(this short[] rowVector, int[][] matrix, double[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this short[][] a, int[,] b, double[][] result) + public static double[][] DotWithTransposed(this short[][] a, int[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -128103,19 +129826,22 @@ public static unsafe double[][] DotWithTransposed(this short[][] a, int[,] b, do #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - short[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + short[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -128137,7 +129863,7 @@ public static unsafe double[][] DotWithTransposed(this short[][] a, int[,] b, do #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this short[,] a, int[][] b, double[][] result) + public static double[][] DotWithTransposed(this short[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -128147,20 +129873,22 @@ public static unsafe double[][] DotWithTransposed(this short[,] a, int[][] b, do #endif int n = a.Rows(); - fixed (short* A = a) - for (int j = 0; j < b.Length; j++) - { - short* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (short* A = a) + for (int j = 0; j < b.Length; j++) + { + short* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -128306,7 +130034,7 @@ public static double[][] DotWithTransposed(this short[][] a, int[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this short[,] a, int[,] b, double[,] result) + public static double[,] TransposeAndDot(this short[,] a, int[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -128317,35 +130045,37 @@ public static double[][] DotWithTransposed(this short[][] a, int[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (int* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (int* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - short aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + short aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -128739,18 +130469,22 @@ public static double[][] DivideByDiagonal(this short[][] a, int[] diagonal, doub #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this short[] a, int[] b, double[,] result) + public static double[,] Outer(this short[] a, int[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -128913,25 +130647,28 @@ public static double[][] Kronecker(this short[][] a, int[][] b, double[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this short[][] a, int[,] b, double[][] result) + public static double[][] Kronecker(this short[][] a, int[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -128985,18 +130722,22 @@ public static double[][] Kronecker(this short[,] a, int[][] b, double[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this short[] a, int[] b, double[] result) + public static double[] Kronecker(this short[] a, int[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -129190,7 +130931,7 @@ public static short[][] Dot(this short[][] a, float[,] b, short[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] Dot(this short[,] a, float[][] b, short[][] result) + public static short[][] Dot(this short[,] a, float[][] b, short[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -129204,21 +130945,24 @@ public static unsafe short[][] Dot(this short[,] a, float[][] b, short[][] resul var t = new float[K]; - fixed (short* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (short* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - short* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (short)s; - } - } + short* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (short)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -129296,7 +131040,7 @@ public static short[] Dot(this short[][] matrix, float[] columnVector, short[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[] Dot(this short[,] matrix, float[] columnVector, short[] result) + public static short[] Dot(this short[,] matrix, float[] columnVector, short[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -129305,55 +131049,54 @@ public static unsafe short[] Dot(this short[,] matrix, float[] columnVector, sho if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (short* a = matrix) + fixed (float* x = columnVector) + fixed (short* r = result) + { + short* pa1 = a; + short* pa2 = a + cols; + short* pr = r; - fixed (short* a = matrix) - fixed (float* x = columnVector) - fixed (short* r = result) - { - short* pa1 = a; - short* pa2 = a + cols; - short* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (short)sum1; - *pr++ = (short)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (short)sum1; + *pr++ = (short)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (short)sum; - } - } + *pr = (short)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -129459,7 +131202,7 @@ public static short[] Dot(this short[] rowVector, float[][] matrix, short[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] DotWithTransposed(this short[][] a, float[,] b, short[][] result) + public static short[][] DotWithTransposed(this short[][] a, float[,] b, short[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -129469,19 +131212,22 @@ public static unsafe short[][] DotWithTransposed(this short[][] a, float[,] b, s #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - short[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (short)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + short[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (short)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -129503,7 +131249,7 @@ public static unsafe short[][] DotWithTransposed(this short[][] a, float[,] b, s #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] DotWithTransposed(this short[,] a, float[][] b, short[][] result) + public static short[][] DotWithTransposed(this short[,] a, float[][] b, short[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -129513,20 +131259,22 @@ public static unsafe short[][] DotWithTransposed(this short[,] a, float[][] b, s #endif int n = a.Rows(); - fixed (short* A = a) - for (int j = 0; j < b.Length; j++) - { - short* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (short)sum; - } - } - + unsafe + { + fixed (short* A = a) + for (int j = 0; j < b.Length; j++) + { + short* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (short)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -129672,7 +131420,7 @@ public static short[][] DotWithTransposed(this short[][] a, float[] columnVector #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[,] TransposeAndDot(this short[,] a, float[,] b, short[,] result) + public static short[,] TransposeAndDot(this short[,] a, float[,] b, short[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -129683,35 +131431,37 @@ public static short[][] DotWithTransposed(this short[][] a, float[] columnVector throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (short* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + short* pr = R; - fixed (short* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - short* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - short aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (short)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + short aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (short)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -130105,18 +131855,22 @@ public static short[][] DivideByDiagonal(this short[][] a, float[] diagonal, sho #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[,] Outer(this short[] a, float[] b, short[,] result) + public static short[,] Outer(this short[] a, float[] b, short[,] result) { - fixed (short* R = result) - { - short* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (short)(x * (double)b[j]); - } - } + unsafe + { + fixed (short* R = result) + { + short* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (short)(x * (double)b[j]); + } + } + } + return result; } @@ -130279,25 +132033,28 @@ public static short[][] Kronecker(this short[][] a, float[][] b, short[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[][] Kronecker(this short[][] a, float[,] b, short[][] result) + public static short[][] Kronecker(this short[][] a, float[,] b, short[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (short)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (short)(aval * (double)(*pb++)); + } + } + return result; } @@ -130351,18 +132108,22 @@ public static short[][] Kronecker(this short[,] a, float[][] b, short[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe short[] Kronecker(this short[] a, float[] b, short[] result) + public static short[] Kronecker(this short[] a, float[] b, short[] result) { - fixed (short* R = result) - { - short* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (short)(x * (double)b[j]); - } - } + unsafe + { + fixed (short* R = result) + { + short* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (short)(x * (double)b[j]); + } + } + } + return result; } @@ -130556,7 +132317,7 @@ public static float[][] Dot(this short[][] a, float[,] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Dot(this short[,] a, float[][] b, float[][] result) + public static float[][] Dot(this short[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -130570,21 +132331,24 @@ public static unsafe float[][] Dot(this short[,] a, float[][] b, float[][] resul var t = new float[K]; - fixed (short* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (short* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - short* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (float)s; - } - } + short* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (float)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -130662,7 +132426,7 @@ public static float[] Dot(this short[][] matrix, float[] columnVector, float[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Dot(this short[,] matrix, float[] columnVector, float[] result) + public static float[] Dot(this short[,] matrix, float[] columnVector, float[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -130671,55 +132435,54 @@ public static unsafe float[] Dot(this short[,] matrix, float[] columnVector, flo if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (short* a = matrix) + fixed (float* x = columnVector) + fixed (float* r = result) + { + short* pa1 = a; + short* pa2 = a + cols; + float* pr = r; - fixed (short* a = matrix) - fixed (float* x = columnVector) - fixed (float* r = result) - { - short* pa1 = a; - short* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (float)sum1; - *pr++ = (float)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (float)sum1; + *pr++ = (float)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (float)sum; - } - } + *pr = (float)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -130825,7 +132588,7 @@ public static float[] Dot(this short[] rowVector, float[][] matrix, float[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this short[][] a, float[,] b, float[][] result) + public static float[][] DotWithTransposed(this short[][] a, float[,] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -130835,19 +132598,22 @@ public static unsafe float[][] DotWithTransposed(this short[][] a, float[,] b, f #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - short[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (float)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + short[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -130869,7 +132635,7 @@ public static unsafe float[][] DotWithTransposed(this short[][] a, float[,] b, f #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this short[,] a, float[][] b, float[][] result) + public static float[][] DotWithTransposed(this short[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -130879,20 +132645,22 @@ public static unsafe float[][] DotWithTransposed(this short[,] a, float[][] b, f #endif int n = a.Rows(); - fixed (short* A = a) - for (int j = 0; j < b.Length; j++) - { - short* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (float)sum; - } - } - + unsafe + { + fixed (short* A = a) + for (int j = 0; j < b.Length; j++) + { + short* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -131038,7 +132806,7 @@ public static float[][] DotWithTransposed(this short[][] a, float[] columnVector #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] TransposeAndDot(this short[,] a, float[,] b, float[,] result) + public static float[,] TransposeAndDot(this short[,] a, float[,] b, float[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -131049,35 +132817,37 @@ public static float[][] DotWithTransposed(this short[][] a, float[] columnVector throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (float* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + float* pr = R; - fixed (float* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - float* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - short aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (float)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + short aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (float)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -131471,18 +133241,22 @@ public static float[][] DivideByDiagonal(this short[][] a, float[] diagonal, flo #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] Outer(this short[] a, float[] b, float[,] result) + public static float[,] Outer(this short[] a, float[] b, float[,] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -131645,25 +133419,28 @@ public static float[][] Kronecker(this short[][] a, float[][] b, float[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Kronecker(this short[][] a, float[,] b, float[][] result) + public static float[][] Kronecker(this short[][] a, float[,] b, float[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); + } + } + return result; } @@ -131717,18 +133494,22 @@ public static float[][] Kronecker(this short[,] a, float[][] b, float[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Kronecker(this short[] a, float[] b, float[] result) + public static float[] Kronecker(this short[] a, float[] b, float[] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -131922,7 +133703,7 @@ public static double[][] Dot(this short[][] a, float[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this short[,] a, float[][] b, double[][] result) + public static double[][] Dot(this short[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -131936,21 +133717,24 @@ public static unsafe double[][] Dot(this short[,] a, float[][] b, double[][] res var t = new float[K]; - fixed (short* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (short* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - short* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + short* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -132028,7 +133812,7 @@ public static double[] Dot(this short[][] matrix, float[] columnVector, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this short[,] matrix, float[] columnVector, double[] result) + public static double[] Dot(this short[,] matrix, float[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -132037,55 +133821,54 @@ public static unsafe double[] Dot(this short[,] matrix, float[] columnVector, do if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (short* a = matrix) + fixed (float* x = columnVector) + fixed (double* r = result) + { + short* pa1 = a; + short* pa2 = a + cols; + double* pr = r; - fixed (short* a = matrix) - fixed (float* x = columnVector) - fixed (double* r = result) - { - short* pa1 = a; - short* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -132191,7 +133974,7 @@ public static double[] Dot(this short[] rowVector, float[][] matrix, double[] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this short[][] a, float[,] b, double[][] result) + public static double[][] DotWithTransposed(this short[][] a, float[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -132201,19 +133984,22 @@ public static unsafe double[][] DotWithTransposed(this short[][] a, float[,] b, #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - short[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + short[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -132235,7 +134021,7 @@ public static unsafe double[][] DotWithTransposed(this short[][] a, float[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this short[,] a, float[][] b, double[][] result) + public static double[][] DotWithTransposed(this short[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -132245,20 +134031,22 @@ public static unsafe double[][] DotWithTransposed(this short[,] a, float[][] b, #endif int n = a.Rows(); - fixed (short* A = a) - for (int j = 0; j < b.Length; j++) - { - short* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (short* A = a) + for (int j = 0; j < b.Length; j++) + { + short* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -132404,7 +134192,7 @@ public static double[][] DotWithTransposed(this short[][] a, float[] columnVecto #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this short[,] a, float[,] b, double[,] result) + public static double[,] TransposeAndDot(this short[,] a, float[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -132415,35 +134203,37 @@ public static double[][] DotWithTransposed(this short[][] a, float[] columnVecto throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - short aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + short aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -132837,18 +134627,22 @@ public static double[][] DivideByDiagonal(this short[][] a, float[] diagonal, do #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this short[] a, float[] b, double[,] result) + public static double[,] Outer(this short[] a, float[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -133011,25 +134805,28 @@ public static double[][] Kronecker(this short[][] a, float[][] b, double[][] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this short[][] a, float[,] b, double[][] result) + public static double[][] Kronecker(this short[][] a, float[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -133083,18 +134880,22 @@ public static double[][] Kronecker(this short[,] a, float[][] b, double[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this short[] a, float[] b, double[] result) + public static double[] Kronecker(this short[] a, float[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -133288,7 +135089,7 @@ public static int[][] Dot(this short[][] a, float[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this short[,] a, float[][] b, int[][] result) + public static int[][] Dot(this short[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -133302,21 +135103,24 @@ public static unsafe int[][] Dot(this short[,] a, float[][] b, int[][] result) var t = new float[K]; - fixed (short* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (short* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - short* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (int)s; - } - } + short* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -133394,7 +135198,7 @@ public static int[] Dot(this short[][] matrix, float[] columnVector, int[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this short[,] matrix, float[] columnVector, int[] result) + public static int[] Dot(this short[,] matrix, float[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -133403,55 +135207,54 @@ public static unsafe int[] Dot(this short[,] matrix, float[] columnVector, int[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (short* a = matrix) + fixed (float* x = columnVector) + fixed (int* r = result) + { + short* pa1 = a; + short* pa2 = a + cols; + int* pr = r; - fixed (short* a = matrix) - fixed (float* x = columnVector) - fixed (int* r = result) - { - short* pa1 = a; - short* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -133557,7 +135360,7 @@ public static int[] Dot(this short[] rowVector, float[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this short[][] a, float[,] b, int[][] result) + public static int[][] DotWithTransposed(this short[][] a, float[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -133567,19 +135370,22 @@ public static unsafe int[][] DotWithTransposed(this short[][] a, float[,] b, int #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - short[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + short[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -133601,7 +135407,7 @@ public static unsafe int[][] DotWithTransposed(this short[][] a, float[,] b, int #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this short[,] a, float[][] b, int[][] result) + public static int[][] DotWithTransposed(this short[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -133611,20 +135417,22 @@ public static unsafe int[][] DotWithTransposed(this short[,] a, float[][] b, int #endif int n = a.Rows(); - fixed (short* A = a) - for (int j = 0; j < b.Length; j++) - { - short* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (short* A = a) + for (int j = 0; j < b.Length; j++) + { + short* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -133770,7 +135578,7 @@ public static int[][] DotWithTransposed(this short[][] a, float[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this short[,] a, float[,] b, int[,] result) + public static int[,] TransposeAndDot(this short[,] a, float[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -133781,35 +135589,37 @@ public static int[][] DotWithTransposed(this short[][] a, float[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - short aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + short aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -134203,18 +136013,22 @@ public static int[][] DivideByDiagonal(this short[][] a, float[] diagonal, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this short[] a, float[] b, int[,] result) + public static int[,] Outer(this short[] a, float[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -134377,25 +136191,28 @@ public static int[][] Kronecker(this short[][] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this short[][] a, float[,] b, int[][] result) + public static int[][] Kronecker(this short[][] a, float[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); + } + } + return result; } @@ -134449,18 +136266,22 @@ public static int[][] Kronecker(this short[,] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this short[] a, float[] b, int[] result) + public static int[] Kronecker(this short[] a, float[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -134654,7 +136475,7 @@ public static sbyte[][] Dot(this sbyte[][] a, sbyte[,] b, sbyte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] Dot(this sbyte[,] a, sbyte[][] b, sbyte[][] result) + public static sbyte[][] Dot(this sbyte[,] a, sbyte[][] b, sbyte[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -134668,21 +136489,24 @@ public static unsafe sbyte[][] Dot(this sbyte[,] a, sbyte[][] b, sbyte[][] resul var t = new sbyte[K]; - fixed (sbyte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - sbyte* pa = A; - for (int i = 0; i < N; i++) - { - sbyte s = (sbyte)0; - for (int k = 0; k < t.Length; k++) - s += (sbyte)((sbyte)(*pa++) * (sbyte)t[k]); - result[i][j] = (sbyte)s; - } - } + sbyte* pa = A; + for (int i = 0; i < N; i++) + { + sbyte s = (sbyte)0; + for (int k = 0; k < t.Length; k++) + s += (sbyte)((sbyte)(*pa++) * (sbyte)t[k]); + result[i][j] = (sbyte)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -134760,7 +136584,7 @@ public static sbyte[] Dot(this sbyte[][] matrix, sbyte[] columnVector, sbyte[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[] Dot(this sbyte[,] matrix, sbyte[] columnVector, sbyte[] result) + public static sbyte[] Dot(this sbyte[,] matrix, sbyte[] columnVector, sbyte[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -134769,55 +136593,54 @@ public static unsafe sbyte[] Dot(this sbyte[,] matrix, sbyte[] columnVector, sby if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (sbyte* a = matrix) + fixed (sbyte* x = columnVector) + fixed (sbyte* r = result) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + sbyte* pr = r; - fixed (sbyte* a = matrix) - fixed (sbyte* x = columnVector) - fixed (sbyte* r = result) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - sbyte sum1 = 0, sum2 = 0; - sbyte* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (sbyte)((sbyte)(*pa1++) * (sbyte)(*px)); - sum2 += (sbyte)((sbyte)(*pa2++) * (sbyte)(*px)); - px++; - } - - *pr++ = (sbyte)sum1; - *pr++ = (sbyte)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + sbyte sum1 = 0, sum2 = 0; + sbyte* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (sbyte)((sbyte)(*pa1++) * (sbyte)(*px)); + sum2 += (sbyte)((sbyte)(*pa2++) * (sbyte)(*px)); + px++; + } + + *pr++ = (sbyte)sum1; + *pr++ = (sbyte)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - sbyte sum = 0; - sbyte* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + sbyte sum = 0; + sbyte* px = x; - for (int j = 0; j < cols; j++) - sum += (sbyte)((sbyte)(*pa1++) * (sbyte)(*px++)); + for (int j = 0; j < cols; j++) + sum += (sbyte)((sbyte)(*pa1++) * (sbyte)(*px++)); - *pr = (sbyte)sum; - } - } + *pr = (sbyte)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -134923,7 +136746,7 @@ public static sbyte[] Dot(this sbyte[] rowVector, sbyte[][] matrix, sbyte[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] DotWithTransposed(this sbyte[][] a, sbyte[,] b, sbyte[][] result) + public static sbyte[][] DotWithTransposed(this sbyte[][] a, sbyte[,] b, sbyte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -134933,19 +136756,22 @@ public static unsafe sbyte[][] DotWithTransposed(this sbyte[][] a, sbyte[,] b, s #endif int n = b.Rows(); - fixed (sbyte* B = b) - for (int i = 0; i < a.Length; i++) - { - sbyte* pb = B; - sbyte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - sbyte sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (sbyte)((sbyte)arow[k] * (sbyte)(*pb++)); - result[i][j] = (sbyte)sum; - } - } + unsafe + { + fixed (sbyte* B = b) + for (int i = 0; i < a.Length; i++) + { + sbyte* pb = B; + sbyte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + sbyte sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (sbyte)((sbyte)arow[k] * (sbyte)(*pb++)); + result[i][j] = (sbyte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -134967,7 +136793,7 @@ public static unsafe sbyte[][] DotWithTransposed(this sbyte[][] a, sbyte[,] b, s #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] DotWithTransposed(this sbyte[,] a, sbyte[][] b, sbyte[][] result) + public static sbyte[][] DotWithTransposed(this sbyte[,] a, sbyte[][] b, sbyte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -134977,20 +136803,22 @@ public static unsafe sbyte[][] DotWithTransposed(this sbyte[,] a, sbyte[][] b, s #endif int n = a.Rows(); - fixed (sbyte* A = a) - for (int j = 0; j < b.Length; j++) - { - sbyte* pa = A; - for (int i = 0; i < n; i++) - { - sbyte sum = 0; - sbyte[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (sbyte)((sbyte)(*pa++) * (sbyte)brow[k]); - result[i][j] = (sbyte)sum; - } - } - + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < b.Length; j++) + { + sbyte* pa = A; + for (int i = 0; i < n; i++) + { + sbyte sum = 0; + sbyte[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (sbyte)((sbyte)(*pa++) * (sbyte)brow[k]); + result[i][j] = (sbyte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -135136,7 +136964,7 @@ public static sbyte[][] DotWithTransposed(this sbyte[][] a, sbyte[] columnVector #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[,] TransposeAndDot(this sbyte[,] a, sbyte[,] b, sbyte[,] result) + public static sbyte[,] TransposeAndDot(this sbyte[,] a, sbyte[,] b, sbyte[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -135147,35 +136975,37 @@ public static sbyte[][] DotWithTransposed(this sbyte[][] a, sbyte[] columnVector throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (sbyte* R = result) + fixed (sbyte* B = b) + fixed (sbyte* ptemp = new sbyte[p]) + { + sbyte* pr = R; - fixed (sbyte* R = result) - fixed (sbyte* B = b) - fixed (sbyte* ptemp = new sbyte[p]) - { - sbyte* pr = R; - - for (int i = 0; i < m; i++) - { - sbyte* pt = ptemp; - sbyte* pb = B; - - for (int k = 0; k < n; k++) - { - sbyte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (sbyte)((sbyte)aval * (sbyte)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (sbyte)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + sbyte* pt = ptemp; + sbyte* pb = B; + + for (int k = 0; k < n; k++) + { + sbyte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (sbyte)((sbyte)aval * (sbyte)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (sbyte)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -135569,18 +137399,22 @@ public static sbyte[][] DivideByDiagonal(this sbyte[][] a, sbyte[] diagonal, sby #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[,] Outer(this sbyte[] a, sbyte[] b, sbyte[,] result) + public static sbyte[,] Outer(this sbyte[] a, sbyte[] b, sbyte[,] result) { - fixed (sbyte* R = result) - { - sbyte* pr = R; - for (int i = 0; i < a.Length; i++) - { - sbyte x = (sbyte)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (sbyte)(x * (sbyte)b[j]); - } - } + unsafe + { + fixed (sbyte* R = result) + { + sbyte* pr = R; + for (int i = 0; i < a.Length; i++) + { + sbyte x = (sbyte)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (sbyte)(x * (sbyte)b[j]); + } + } + } + return result; } @@ -135743,25 +137577,28 @@ public static sbyte[][] Kronecker(this sbyte[][] a, sbyte[][] b, sbyte[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] Kronecker(this sbyte[][] a, sbyte[,] b, sbyte[][] result) + public static sbyte[][] Kronecker(this sbyte[][] a, sbyte[,] b, sbyte[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (sbyte* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - sbyte aval = (sbyte)a[i][j]; - sbyte* pb = B; + unsafe + { + fixed (sbyte* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + sbyte aval = (sbyte)a[i][j]; + sbyte* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (sbyte)(aval * (sbyte)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (sbyte)(aval * (sbyte)(*pb++)); + } + } + return result; } @@ -135815,18 +137652,22 @@ public static sbyte[][] Kronecker(this sbyte[,] a, sbyte[][] b, sbyte[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[] Kronecker(this sbyte[] a, sbyte[] b, sbyte[] result) + public static sbyte[] Kronecker(this sbyte[] a, sbyte[] b, sbyte[] result) { - fixed (sbyte* R = result) - { - sbyte* pr = R; - for (int i = 0; i < a.Length; i++) - { - sbyte x = (sbyte)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (sbyte)(x * (sbyte)b[j]); - } - } + unsafe + { + fixed (sbyte* R = result) + { + sbyte* pr = R; + for (int i = 0; i < a.Length; i++) + { + sbyte x = (sbyte)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (sbyte)(x * (sbyte)b[j]); + } + } + } + return result; } @@ -136020,7 +137861,7 @@ public static double[][] Dot(this sbyte[][] a, sbyte[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this sbyte[,] a, sbyte[][] b, double[][] result) + public static double[][] Dot(this sbyte[,] a, sbyte[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -136034,21 +137875,24 @@ public static unsafe double[][] Dot(this sbyte[,] a, sbyte[][] b, double[][] res var t = new sbyte[K]; - fixed (sbyte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - sbyte* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + sbyte* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -136126,7 +137970,7 @@ public static double[] Dot(this sbyte[][] matrix, sbyte[] columnVector, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this sbyte[,] matrix, sbyte[] columnVector, double[] result) + public static double[] Dot(this sbyte[,] matrix, sbyte[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -136135,55 +137979,54 @@ public static unsafe double[] Dot(this sbyte[,] matrix, sbyte[] columnVector, do if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (sbyte* a = matrix) + fixed (sbyte* x = columnVector) + fixed (double* r = result) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + double* pr = r; - fixed (sbyte* a = matrix) - fixed (sbyte* x = columnVector) - fixed (double* r = result) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - sbyte* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + sbyte* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - sbyte* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + sbyte* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -136289,7 +138132,7 @@ public static double[] Dot(this sbyte[] rowVector, sbyte[][] matrix, double[] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this sbyte[][] a, sbyte[,] b, double[][] result) + public static double[][] DotWithTransposed(this sbyte[][] a, sbyte[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -136299,19 +138142,22 @@ public static unsafe double[][] DotWithTransposed(this sbyte[][] a, sbyte[,] b, #endif int n = b.Rows(); - fixed (sbyte* B = b) - for (int i = 0; i < a.Length; i++) - { - sbyte* pb = B; - sbyte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (sbyte* B = b) + for (int i = 0; i < a.Length; i++) + { + sbyte* pb = B; + sbyte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -136333,7 +138179,7 @@ public static unsafe double[][] DotWithTransposed(this sbyte[][] a, sbyte[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this sbyte[,] a, sbyte[][] b, double[][] result) + public static double[][] DotWithTransposed(this sbyte[,] a, sbyte[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -136343,20 +138189,22 @@ public static unsafe double[][] DotWithTransposed(this sbyte[,] a, sbyte[][] b, #endif int n = a.Rows(); - fixed (sbyte* A = a) - for (int j = 0; j < b.Length; j++) - { - sbyte* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - sbyte[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < b.Length; j++) + { + sbyte* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + sbyte[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -136502,7 +138350,7 @@ public static double[][] DotWithTransposed(this sbyte[][] a, sbyte[] columnVecto #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this sbyte[,] a, sbyte[,] b, double[,] result) + public static double[,] TransposeAndDot(this sbyte[,] a, sbyte[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -136513,35 +138361,37 @@ public static double[][] DotWithTransposed(this sbyte[][] a, sbyte[] columnVecto throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (sbyte* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (sbyte* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - sbyte* pb = B; - - for (int k = 0; k < n; k++) - { - sbyte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + sbyte* pb = B; + + for (int k = 0; k < n; k++) + { + sbyte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -136935,18 +138785,22 @@ public static double[][] DivideByDiagonal(this sbyte[][] a, sbyte[] diagonal, do #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this sbyte[] a, sbyte[] b, double[,] result) + public static double[,] Outer(this sbyte[] a, sbyte[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -137109,25 +138963,28 @@ public static double[][] Kronecker(this sbyte[][] a, sbyte[][] b, double[][] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this sbyte[][] a, sbyte[,] b, double[][] result) + public static double[][] Kronecker(this sbyte[][] a, sbyte[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (sbyte* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - sbyte* pb = B; + unsafe + { + fixed (sbyte* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + sbyte* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -137181,18 +139038,22 @@ public static double[][] Kronecker(this sbyte[,] a, sbyte[][] b, double[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this sbyte[] a, sbyte[] b, double[] result) + public static double[] Kronecker(this sbyte[] a, sbyte[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -137386,7 +139247,7 @@ public static int[][] Dot(this sbyte[][] a, sbyte[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this sbyte[,] a, sbyte[][] b, int[][] result) + public static int[][] Dot(this sbyte[,] a, sbyte[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -137400,21 +139261,24 @@ public static unsafe int[][] Dot(this sbyte[,] a, sbyte[][] b, int[][] result) var t = new sbyte[K]; - fixed (sbyte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - sbyte* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + sbyte* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -137492,7 +139356,7 @@ public static int[] Dot(this sbyte[][] matrix, sbyte[] columnVector, int[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this sbyte[,] matrix, sbyte[] columnVector, int[] result) + public static int[] Dot(this sbyte[,] matrix, sbyte[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -137501,55 +139365,54 @@ public static unsafe int[] Dot(this sbyte[,] matrix, sbyte[] columnVector, int[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (sbyte* a = matrix) + fixed (sbyte* x = columnVector) + fixed (int* r = result) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + int* pr = r; - fixed (sbyte* a = matrix) - fixed (sbyte* x = columnVector) - fixed (int* r = result) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - sbyte* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + sbyte* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - sbyte* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + sbyte* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -137655,7 +139518,7 @@ public static int[] Dot(this sbyte[] rowVector, sbyte[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this sbyte[][] a, sbyte[,] b, int[][] result) + public static int[][] DotWithTransposed(this sbyte[][] a, sbyte[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -137665,19 +139528,22 @@ public static unsafe int[][] DotWithTransposed(this sbyte[][] a, sbyte[,] b, int #endif int n = b.Rows(); - fixed (sbyte* B = b) - for (int i = 0; i < a.Length; i++) - { - sbyte* pb = B; - sbyte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (sbyte* B = b) + for (int i = 0; i < a.Length; i++) + { + sbyte* pb = B; + sbyte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -137699,7 +139565,7 @@ public static unsafe int[][] DotWithTransposed(this sbyte[][] a, sbyte[,] b, int #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this sbyte[,] a, sbyte[][] b, int[][] result) + public static int[][] DotWithTransposed(this sbyte[,] a, sbyte[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -137709,20 +139575,22 @@ public static unsafe int[][] DotWithTransposed(this sbyte[,] a, sbyte[][] b, int #endif int n = a.Rows(); - fixed (sbyte* A = a) - for (int j = 0; j < b.Length; j++) - { - sbyte* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - sbyte[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < b.Length; j++) + { + sbyte* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + sbyte[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -137868,7 +139736,7 @@ public static int[][] DotWithTransposed(this sbyte[][] a, sbyte[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this sbyte[,] a, sbyte[,] b, int[,] result) + public static int[,] TransposeAndDot(this sbyte[,] a, sbyte[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -137879,35 +139747,37 @@ public static int[][] DotWithTransposed(this sbyte[][] a, sbyte[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (sbyte* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (sbyte* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - sbyte* pb = B; - - for (int k = 0; k < n; k++) - { - sbyte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + sbyte* pb = B; + + for (int k = 0; k < n; k++) + { + sbyte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -138301,18 +140171,22 @@ public static int[][] DivideByDiagonal(this sbyte[][] a, sbyte[] diagonal, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this sbyte[] a, sbyte[] b, int[,] result) + public static int[,] Outer(this sbyte[] a, sbyte[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -138475,25 +140349,28 @@ public static int[][] Kronecker(this sbyte[][] a, sbyte[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this sbyte[][] a, sbyte[,] b, int[][] result) + public static int[][] Kronecker(this sbyte[][] a, sbyte[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (sbyte* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - sbyte* pb = B; + unsafe + { + fixed (sbyte* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + sbyte* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -138547,18 +140424,22 @@ public static int[][] Kronecker(this sbyte[,] a, sbyte[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this sbyte[] a, sbyte[] b, int[] result) + public static int[] Kronecker(this sbyte[] a, sbyte[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -138752,7 +140633,7 @@ public static sbyte[][] Dot(this sbyte[][] a, double[,] b, sbyte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] Dot(this sbyte[,] a, double[][] b, sbyte[][] result) + public static sbyte[][] Dot(this sbyte[,] a, double[][] b, sbyte[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -138766,21 +140647,24 @@ public static unsafe sbyte[][] Dot(this sbyte[,] a, double[][] b, sbyte[][] resu var t = new double[K]; - fixed (sbyte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - sbyte* pa = A; - for (int i = 0; i < N; i++) - { - sbyte s = (sbyte)0; - for (int k = 0; k < t.Length; k++) - s += (sbyte)((sbyte)(*pa++) * (sbyte)t[k]); - result[i][j] = (sbyte)s; - } - } + sbyte* pa = A; + for (int i = 0; i < N; i++) + { + sbyte s = (sbyte)0; + for (int k = 0; k < t.Length; k++) + s += (sbyte)((sbyte)(*pa++) * (sbyte)t[k]); + result[i][j] = (sbyte)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -138858,7 +140742,7 @@ public static sbyte[] Dot(this sbyte[][] matrix, double[] columnVector, sbyte[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[] Dot(this sbyte[,] matrix, double[] columnVector, sbyte[] result) + public static sbyte[] Dot(this sbyte[,] matrix, double[] columnVector, sbyte[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -138867,55 +140751,54 @@ public static unsafe sbyte[] Dot(this sbyte[,] matrix, double[] columnVector, sb if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (sbyte* a = matrix) + fixed (double* x = columnVector) + fixed (sbyte* r = result) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + sbyte* pr = r; - fixed (sbyte* a = matrix) - fixed (double* x = columnVector) - fixed (sbyte* r = result) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - sbyte sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (sbyte)((sbyte)(*pa1++) * (sbyte)(*px)); - sum2 += (sbyte)((sbyte)(*pa2++) * (sbyte)(*px)); - px++; - } - - *pr++ = (sbyte)sum1; - *pr++ = (sbyte)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + sbyte sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (sbyte)((sbyte)(*pa1++) * (sbyte)(*px)); + sum2 += (sbyte)((sbyte)(*pa2++) * (sbyte)(*px)); + px++; + } + + *pr++ = (sbyte)sum1; + *pr++ = (sbyte)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - sbyte sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + sbyte sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (sbyte)((sbyte)(*pa1++) * (sbyte)(*px++)); + for (int j = 0; j < cols; j++) + sum += (sbyte)((sbyte)(*pa1++) * (sbyte)(*px++)); - *pr = (sbyte)sum; - } - } + *pr = (sbyte)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -139021,7 +140904,7 @@ public static sbyte[] Dot(this sbyte[] rowVector, double[][] matrix, sbyte[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] DotWithTransposed(this sbyte[][] a, double[,] b, sbyte[][] result) + public static sbyte[][] DotWithTransposed(this sbyte[][] a, double[,] b, sbyte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -139031,19 +140914,22 @@ public static unsafe sbyte[][] DotWithTransposed(this sbyte[][] a, double[,] b, #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - sbyte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - sbyte sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (sbyte)((sbyte)arow[k] * (sbyte)(*pb++)); - result[i][j] = (sbyte)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + sbyte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + sbyte sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (sbyte)((sbyte)arow[k] * (sbyte)(*pb++)); + result[i][j] = (sbyte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -139065,7 +140951,7 @@ public static unsafe sbyte[][] DotWithTransposed(this sbyte[][] a, double[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] DotWithTransposed(this sbyte[,] a, double[][] b, sbyte[][] result) + public static sbyte[][] DotWithTransposed(this sbyte[,] a, double[][] b, sbyte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -139075,20 +140961,22 @@ public static unsafe sbyte[][] DotWithTransposed(this sbyte[,] a, double[][] b, #endif int n = a.Rows(); - fixed (sbyte* A = a) - for (int j = 0; j < b.Length; j++) - { - sbyte* pa = A; - for (int i = 0; i < n; i++) - { - sbyte sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (sbyte)((sbyte)(*pa++) * (sbyte)brow[k]); - result[i][j] = (sbyte)sum; - } - } - + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < b.Length; j++) + { + sbyte* pa = A; + for (int i = 0; i < n; i++) + { + sbyte sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (sbyte)((sbyte)(*pa++) * (sbyte)brow[k]); + result[i][j] = (sbyte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -139234,7 +141122,7 @@ public static sbyte[][] DotWithTransposed(this sbyte[][] a, double[] columnVecto #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[,] TransposeAndDot(this sbyte[,] a, double[,] b, sbyte[,] result) + public static sbyte[,] TransposeAndDot(this sbyte[,] a, double[,] b, sbyte[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -139245,35 +141133,37 @@ public static sbyte[][] DotWithTransposed(this sbyte[][] a, double[] columnVecto throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (sbyte* R = result) + fixed (double* B = b) + fixed (sbyte* ptemp = new sbyte[p]) + { + sbyte* pr = R; - fixed (sbyte* R = result) - fixed (double* B = b) - fixed (sbyte* ptemp = new sbyte[p]) - { - sbyte* pr = R; - - for (int i = 0; i < m; i++) - { - sbyte* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - sbyte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (sbyte)((sbyte)aval * (sbyte)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (sbyte)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + sbyte* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + sbyte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (sbyte)((sbyte)aval * (sbyte)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (sbyte)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -139667,18 +141557,22 @@ public static sbyte[][] DivideByDiagonal(this sbyte[][] a, double[] diagonal, sb #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[,] Outer(this sbyte[] a, double[] b, sbyte[,] result) + public static sbyte[,] Outer(this sbyte[] a, double[] b, sbyte[,] result) { - fixed (sbyte* R = result) - { - sbyte* pr = R; - for (int i = 0; i < a.Length; i++) - { - sbyte x = (sbyte)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (sbyte)(x * (sbyte)b[j]); - } - } + unsafe + { + fixed (sbyte* R = result) + { + sbyte* pr = R; + for (int i = 0; i < a.Length; i++) + { + sbyte x = (sbyte)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (sbyte)(x * (sbyte)b[j]); + } + } + } + return result; } @@ -139841,25 +141735,28 @@ public static sbyte[][] Kronecker(this sbyte[][] a, double[][] b, sbyte[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] Kronecker(this sbyte[][] a, double[,] b, sbyte[][] result) + public static sbyte[][] Kronecker(this sbyte[][] a, double[,] b, sbyte[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - sbyte aval = (sbyte)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + sbyte aval = (sbyte)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (sbyte)(aval * (sbyte)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (sbyte)(aval * (sbyte)(*pb++)); + } + } + return result; } @@ -139913,18 +141810,22 @@ public static sbyte[][] Kronecker(this sbyte[,] a, double[][] b, sbyte[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[] Kronecker(this sbyte[] a, double[] b, sbyte[] result) + public static sbyte[] Kronecker(this sbyte[] a, double[] b, sbyte[] result) { - fixed (sbyte* R = result) - { - sbyte* pr = R; - for (int i = 0; i < a.Length; i++) - { - sbyte x = (sbyte)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (sbyte)(x * (sbyte)b[j]); - } - } + unsafe + { + fixed (sbyte* R = result) + { + sbyte* pr = R; + for (int i = 0; i < a.Length; i++) + { + sbyte x = (sbyte)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (sbyte)(x * (sbyte)b[j]); + } + } + } + return result; } @@ -140118,7 +142019,7 @@ public static double[][] Dot(this sbyte[][] a, double[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this sbyte[,] a, double[][] b, double[][] result) + public static double[][] Dot(this sbyte[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -140132,21 +142033,24 @@ public static unsafe double[][] Dot(this sbyte[,] a, double[][] b, double[][] re var t = new double[K]; - fixed (sbyte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - sbyte* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + sbyte* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -140224,7 +142128,7 @@ public static double[] Dot(this sbyte[][] matrix, double[] columnVector, double[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this sbyte[,] matrix, double[] columnVector, double[] result) + public static double[] Dot(this sbyte[,] matrix, double[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -140233,55 +142137,54 @@ public static unsafe double[] Dot(this sbyte[,] matrix, double[] columnVector, d if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (sbyte* a = matrix) + fixed (double* x = columnVector) + fixed (double* r = result) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + double* pr = r; - fixed (sbyte* a = matrix) - fixed (double* x = columnVector) - fixed (double* r = result) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -140387,7 +142290,7 @@ public static double[] Dot(this sbyte[] rowVector, double[][] matrix, double[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this sbyte[][] a, double[,] b, double[][] result) + public static double[][] DotWithTransposed(this sbyte[][] a, double[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -140397,19 +142300,22 @@ public static unsafe double[][] DotWithTransposed(this sbyte[][] a, double[,] b, #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - sbyte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + sbyte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -140431,7 +142337,7 @@ public static unsafe double[][] DotWithTransposed(this sbyte[][] a, double[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this sbyte[,] a, double[][] b, double[][] result) + public static double[][] DotWithTransposed(this sbyte[,] a, double[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -140441,20 +142347,22 @@ public static unsafe double[][] DotWithTransposed(this sbyte[,] a, double[][] b, #endif int n = a.Rows(); - fixed (sbyte* A = a) - for (int j = 0; j < b.Length; j++) - { - sbyte* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < b.Length; j++) + { + sbyte* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -140600,7 +142508,7 @@ public static double[][] DotWithTransposed(this sbyte[][] a, double[] columnVect #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this sbyte[,] a, double[,] b, double[,] result) + public static double[,] TransposeAndDot(this sbyte[,] a, double[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -140611,35 +142519,37 @@ public static double[][] DotWithTransposed(this sbyte[][] a, double[] columnVect throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (double* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (double* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - sbyte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + sbyte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -141033,18 +142943,22 @@ public static double[][] DivideByDiagonal(this sbyte[][] a, double[] diagonal, d #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this sbyte[] a, double[] b, double[,] result) + public static double[,] Outer(this sbyte[] a, double[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -141207,25 +143121,28 @@ public static double[][] Kronecker(this sbyte[][] a, double[][] b, double[][] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this sbyte[][] a, double[,] b, double[][] result) + public static double[][] Kronecker(this sbyte[][] a, double[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -141279,18 +143196,22 @@ public static double[][] Kronecker(this sbyte[,] a, double[][] b, double[][] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this sbyte[] a, double[] b, double[] result) + public static double[] Kronecker(this sbyte[] a, double[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -141484,7 +143405,7 @@ public static int[][] Dot(this sbyte[][] a, double[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this sbyte[,] a, double[][] b, int[][] result) + public static int[][] Dot(this sbyte[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -141498,21 +143419,24 @@ public static unsafe int[][] Dot(this sbyte[,] a, double[][] b, int[][] result) var t = new double[K]; - fixed (sbyte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - sbyte* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + sbyte* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -141590,7 +143514,7 @@ public static int[] Dot(this sbyte[][] matrix, double[] columnVector, int[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this sbyte[,] matrix, double[] columnVector, int[] result) + public static int[] Dot(this sbyte[,] matrix, double[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -141599,55 +143523,54 @@ public static unsafe int[] Dot(this sbyte[,] matrix, double[] columnVector, int[ if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (sbyte* a = matrix) + fixed (double* x = columnVector) + fixed (int* r = result) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + int* pr = r; - fixed (sbyte* a = matrix) - fixed (double* x = columnVector) - fixed (int* r = result) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - double* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + double* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - double* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + double* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -141753,7 +143676,7 @@ public static int[] Dot(this sbyte[] rowVector, double[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this sbyte[][] a, double[,] b, int[][] result) + public static int[][] DotWithTransposed(this sbyte[][] a, double[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -141763,19 +143686,22 @@ public static unsafe int[][] DotWithTransposed(this sbyte[][] a, double[,] b, in #endif int n = b.Rows(); - fixed (double* B = b) - for (int i = 0; i < a.Length; i++) - { - double* pb = B; - sbyte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (double* B = b) + for (int i = 0; i < a.Length; i++) + { + double* pb = B; + sbyte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -141797,7 +143723,7 @@ public static unsafe int[][] DotWithTransposed(this sbyte[][] a, double[,] b, in #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this sbyte[,] a, double[][] b, int[][] result) + public static int[][] DotWithTransposed(this sbyte[,] a, double[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -141807,20 +143733,22 @@ public static unsafe int[][] DotWithTransposed(this sbyte[,] a, double[][] b, in #endif int n = a.Rows(); - fixed (sbyte* A = a) - for (int j = 0; j < b.Length; j++) - { - sbyte* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - double[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < b.Length; j++) + { + sbyte* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + double[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -141966,7 +143894,7 @@ public static int[][] DotWithTransposed(this sbyte[][] a, double[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this sbyte[,] a, double[,] b, int[,] result) + public static int[,] TransposeAndDot(this sbyte[,] a, double[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -141977,35 +143905,37 @@ public static int[][] DotWithTransposed(this sbyte[][] a, double[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (double* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (double* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - double* pb = B; - - for (int k = 0; k < n; k++) - { - sbyte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + double* pb = B; + + for (int k = 0; k < n; k++) + { + sbyte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -142399,18 +144329,22 @@ public static int[][] DivideByDiagonal(this sbyte[][] a, double[] diagonal, int[ #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this sbyte[] a, double[] b, int[,] result) + public static int[,] Outer(this sbyte[] a, double[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -142573,25 +144507,28 @@ public static int[][] Kronecker(this sbyte[][] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this sbyte[][] a, double[,] b, int[][] result) + public static int[][] Kronecker(this sbyte[][] a, double[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (double* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - double* pb = B; + unsafe + { + fixed (double* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + double* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -142645,18 +144582,22 @@ public static int[][] Kronecker(this sbyte[,] a, double[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this sbyte[] a, double[] b, int[] result) + public static int[] Kronecker(this sbyte[] a, double[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -142850,7 +144791,7 @@ public static sbyte[][] Dot(this sbyte[][] a, int[,] b, sbyte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] Dot(this sbyte[,] a, int[][] b, sbyte[][] result) + public static sbyte[][] Dot(this sbyte[,] a, int[][] b, sbyte[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -142864,21 +144805,24 @@ public static unsafe sbyte[][] Dot(this sbyte[,] a, int[][] b, sbyte[][] result) var t = new int[K]; - fixed (sbyte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - sbyte* pa = A; - for (int i = 0; i < N; i++) - { - sbyte s = (sbyte)0; - for (int k = 0; k < t.Length; k++) - s += (sbyte)((sbyte)(*pa++) * (sbyte)t[k]); - result[i][j] = (sbyte)s; - } - } + sbyte* pa = A; + for (int i = 0; i < N; i++) + { + sbyte s = (sbyte)0; + for (int k = 0; k < t.Length; k++) + s += (sbyte)((sbyte)(*pa++) * (sbyte)t[k]); + result[i][j] = (sbyte)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -142956,7 +144900,7 @@ public static sbyte[] Dot(this sbyte[][] matrix, int[] columnVector, sbyte[] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[] Dot(this sbyte[,] matrix, int[] columnVector, sbyte[] result) + public static sbyte[] Dot(this sbyte[,] matrix, int[] columnVector, sbyte[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -142965,55 +144909,54 @@ public static unsafe sbyte[] Dot(this sbyte[,] matrix, int[] columnVector, sbyte if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (sbyte* a = matrix) + fixed (int* x = columnVector) + fixed (sbyte* r = result) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + sbyte* pr = r; - fixed (sbyte* a = matrix) - fixed (int* x = columnVector) - fixed (sbyte* r = result) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - sbyte sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (sbyte)((sbyte)(*pa1++) * (sbyte)(*px)); - sum2 += (sbyte)((sbyte)(*pa2++) * (sbyte)(*px)); - px++; - } - - *pr++ = (sbyte)sum1; - *pr++ = (sbyte)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + sbyte sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (sbyte)((sbyte)(*pa1++) * (sbyte)(*px)); + sum2 += (sbyte)((sbyte)(*pa2++) * (sbyte)(*px)); + px++; + } + + *pr++ = (sbyte)sum1; + *pr++ = (sbyte)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - sbyte sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + sbyte sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (sbyte)((sbyte)(*pa1++) * (sbyte)(*px++)); + for (int j = 0; j < cols; j++) + sum += (sbyte)((sbyte)(*pa1++) * (sbyte)(*px++)); - *pr = (sbyte)sum; - } - } + *pr = (sbyte)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -143119,7 +145062,7 @@ public static sbyte[] Dot(this sbyte[] rowVector, int[][] matrix, sbyte[] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] DotWithTransposed(this sbyte[][] a, int[,] b, sbyte[][] result) + public static sbyte[][] DotWithTransposed(this sbyte[][] a, int[,] b, sbyte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -143129,19 +145072,22 @@ public static unsafe sbyte[][] DotWithTransposed(this sbyte[][] a, int[,] b, sby #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - sbyte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - sbyte sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (sbyte)((sbyte)arow[k] * (sbyte)(*pb++)); - result[i][j] = (sbyte)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + sbyte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + sbyte sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (sbyte)((sbyte)arow[k] * (sbyte)(*pb++)); + result[i][j] = (sbyte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -143163,7 +145109,7 @@ public static unsafe sbyte[][] DotWithTransposed(this sbyte[][] a, int[,] b, sby #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] DotWithTransposed(this sbyte[,] a, int[][] b, sbyte[][] result) + public static sbyte[][] DotWithTransposed(this sbyte[,] a, int[][] b, sbyte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -143173,20 +145119,22 @@ public static unsafe sbyte[][] DotWithTransposed(this sbyte[,] a, int[][] b, sby #endif int n = a.Rows(); - fixed (sbyte* A = a) - for (int j = 0; j < b.Length; j++) - { - sbyte* pa = A; - for (int i = 0; i < n; i++) - { - sbyte sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (sbyte)((sbyte)(*pa++) * (sbyte)brow[k]); - result[i][j] = (sbyte)sum; - } - } - + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < b.Length; j++) + { + sbyte* pa = A; + for (int i = 0; i < n; i++) + { + sbyte sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (sbyte)((sbyte)(*pa++) * (sbyte)brow[k]); + result[i][j] = (sbyte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -143332,7 +145280,7 @@ public static sbyte[][] DotWithTransposed(this sbyte[][] a, int[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[,] TransposeAndDot(this sbyte[,] a, int[,] b, sbyte[,] result) + public static sbyte[,] TransposeAndDot(this sbyte[,] a, int[,] b, sbyte[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -143343,35 +145291,37 @@ public static sbyte[][] DotWithTransposed(this sbyte[][] a, int[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (sbyte* R = result) + fixed (int* B = b) + fixed (sbyte* ptemp = new sbyte[p]) + { + sbyte* pr = R; - fixed (sbyte* R = result) - fixed (int* B = b) - fixed (sbyte* ptemp = new sbyte[p]) - { - sbyte* pr = R; - - for (int i = 0; i < m; i++) - { - sbyte* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - sbyte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (sbyte)((sbyte)aval * (sbyte)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (sbyte)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + sbyte* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + sbyte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (sbyte)((sbyte)aval * (sbyte)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (sbyte)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -143765,18 +145715,22 @@ public static sbyte[][] DivideByDiagonal(this sbyte[][] a, int[] diagonal, sbyte #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[,] Outer(this sbyte[] a, int[] b, sbyte[,] result) + public static sbyte[,] Outer(this sbyte[] a, int[] b, sbyte[,] result) { - fixed (sbyte* R = result) - { - sbyte* pr = R; - for (int i = 0; i < a.Length; i++) - { - sbyte x = (sbyte)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (sbyte)(x * (sbyte)b[j]); - } - } + unsafe + { + fixed (sbyte* R = result) + { + sbyte* pr = R; + for (int i = 0; i < a.Length; i++) + { + sbyte x = (sbyte)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (sbyte)(x * (sbyte)b[j]); + } + } + } + return result; } @@ -143939,25 +145893,28 @@ public static sbyte[][] Kronecker(this sbyte[][] a, int[][] b, sbyte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] Kronecker(this sbyte[][] a, int[,] b, sbyte[][] result) + public static sbyte[][] Kronecker(this sbyte[][] a, int[,] b, sbyte[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - sbyte aval = (sbyte)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + sbyte aval = (sbyte)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (sbyte)(aval * (sbyte)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (sbyte)(aval * (sbyte)(*pb++)); + } + } + return result; } @@ -144011,18 +145968,22 @@ public static sbyte[][] Kronecker(this sbyte[,] a, int[][] b, sbyte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[] Kronecker(this sbyte[] a, int[] b, sbyte[] result) + public static sbyte[] Kronecker(this sbyte[] a, int[] b, sbyte[] result) { - fixed (sbyte* R = result) - { - sbyte* pr = R; - for (int i = 0; i < a.Length; i++) - { - sbyte x = (sbyte)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (sbyte)(x * (sbyte)b[j]); - } - } + unsafe + { + fixed (sbyte* R = result) + { + sbyte* pr = R; + for (int i = 0; i < a.Length; i++) + { + sbyte x = (sbyte)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (sbyte)(x * (sbyte)b[j]); + } + } + } + return result; } @@ -144216,7 +146177,7 @@ public static int[][] Dot(this sbyte[][] a, int[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this sbyte[,] a, int[][] b, int[][] result) + public static int[][] Dot(this sbyte[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -144230,21 +146191,24 @@ public static unsafe int[][] Dot(this sbyte[,] a, int[][] b, int[][] result) var t = new int[K]; - fixed (sbyte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - sbyte* pa = A; - for (int i = 0; i < N; i++) - { - int s = (int)0; - for (int k = 0; k < t.Length; k++) - s += (int)((int)(*pa++) * (int)t[k]); - result[i][j] = (int)s; - } - } + sbyte* pa = A; + for (int i = 0; i < N; i++) + { + int s = (int)0; + for (int k = 0; k < t.Length; k++) + s += (int)((int)(*pa++) * (int)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -144322,7 +146286,7 @@ public static int[] Dot(this sbyte[][] matrix, int[] columnVector, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this sbyte[,] matrix, int[] columnVector, int[] result) + public static int[] Dot(this sbyte[,] matrix, int[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -144331,55 +146295,54 @@ public static unsafe int[] Dot(this sbyte[,] matrix, int[] columnVector, int[] r if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (sbyte* a = matrix) + fixed (int* x = columnVector) + fixed (int* r = result) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + int* pr = r; - fixed (sbyte* a = matrix) - fixed (int* x = columnVector) - fixed (int* r = result) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - int sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (int)((int)(*pa1++) * (int)(*px)); - sum2 += (int)((int)(*pa2++) * (int)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + int sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (int)((int)(*pa1++) * (int)(*px)); + sum2 += (int)((int)(*pa2++) * (int)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - int sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + int sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (int)((int)(*pa1++) * (int)(*px++)); + for (int j = 0; j < cols; j++) + sum += (int)((int)(*pa1++) * (int)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -144485,7 +146448,7 @@ public static int[] Dot(this sbyte[] rowVector, int[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this sbyte[][] a, int[,] b, int[][] result) + public static int[][] DotWithTransposed(this sbyte[][] a, int[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -144495,19 +146458,22 @@ public static unsafe int[][] DotWithTransposed(this sbyte[][] a, int[,] b, int[] #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - sbyte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - int sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (int)((int)arow[k] * (int)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + sbyte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + int sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (int)((int)arow[k] * (int)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -144529,7 +146495,7 @@ public static unsafe int[][] DotWithTransposed(this sbyte[][] a, int[,] b, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this sbyte[,] a, int[][] b, int[][] result) + public static int[][] DotWithTransposed(this sbyte[,] a, int[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -144539,20 +146505,22 @@ public static unsafe int[][] DotWithTransposed(this sbyte[,] a, int[][] b, int[] #endif int n = a.Rows(); - fixed (sbyte* A = a) - for (int j = 0; j < b.Length; j++) - { - sbyte* pa = A; - for (int i = 0; i < n; i++) - { - int sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (int)((int)(*pa++) * (int)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < b.Length; j++) + { + sbyte* pa = A; + for (int i = 0; i < n; i++) + { + int sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (int)((int)(*pa++) * (int)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -144698,7 +146666,7 @@ public static int[][] DotWithTransposed(this sbyte[][] a, int[] columnVector, in #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this sbyte[,] a, int[,] b, int[,] result) + public static int[,] TransposeAndDot(this sbyte[,] a, int[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -144709,35 +146677,37 @@ public static int[][] DotWithTransposed(this sbyte[][] a, int[] columnVector, in throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (int* B = b) + fixed (int* ptemp = new int[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (int* B = b) - fixed (int* ptemp = new int[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - int* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - sbyte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (int)((int)aval * (int)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + int* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + sbyte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (int)((int)aval * (int)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -145131,18 +147101,22 @@ public static int[][] DivideByDiagonal(this sbyte[][] a, int[] diagonal, int[][] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this sbyte[] a, int[] b, int[,] result) + public static int[,] Outer(this sbyte[] a, int[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -145305,25 +147279,28 @@ public static int[][] Kronecker(this sbyte[][] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this sbyte[][] a, int[,] b, int[][] result) + public static int[][] Kronecker(this sbyte[][] a, int[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - int aval = (int)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + int aval = (int)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (int)(*pb++)); + } + } + return result; } @@ -145377,18 +147354,22 @@ public static int[][] Kronecker(this sbyte[,] a, int[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this sbyte[] a, int[] b, int[] result) + public static int[] Kronecker(this sbyte[] a, int[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - int x = (int)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (int)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + int x = (int)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (int)b[j]); + } + } + } + return result; } @@ -145582,7 +147563,7 @@ public static double[][] Dot(this sbyte[][] a, int[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this sbyte[,] a, int[][] b, double[][] result) + public static double[][] Dot(this sbyte[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -145596,21 +147577,24 @@ public static unsafe double[][] Dot(this sbyte[,] a, int[][] b, double[][] resul var t = new int[K]; - fixed (sbyte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - sbyte* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + sbyte* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -145688,7 +147672,7 @@ public static double[] Dot(this sbyte[][] matrix, int[] columnVector, double[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this sbyte[,] matrix, int[] columnVector, double[] result) + public static double[] Dot(this sbyte[,] matrix, int[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -145697,55 +147681,54 @@ public static unsafe double[] Dot(this sbyte[,] matrix, int[] columnVector, doub if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (sbyte* a = matrix) + fixed (int* x = columnVector) + fixed (double* r = result) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + double* pr = r; - fixed (sbyte* a = matrix) - fixed (int* x = columnVector) - fixed (double* r = result) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - int* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + int* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - int* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + int* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -145851,7 +147834,7 @@ public static double[] Dot(this sbyte[] rowVector, int[][] matrix, double[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this sbyte[][] a, int[,] b, double[][] result) + public static double[][] DotWithTransposed(this sbyte[][] a, int[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -145861,19 +147844,22 @@ public static unsafe double[][] DotWithTransposed(this sbyte[][] a, int[,] b, do #endif int n = b.Rows(); - fixed (int* B = b) - for (int i = 0; i < a.Length; i++) - { - int* pb = B; - sbyte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (int* B = b) + for (int i = 0; i < a.Length; i++) + { + int* pb = B; + sbyte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -145895,7 +147881,7 @@ public static unsafe double[][] DotWithTransposed(this sbyte[][] a, int[,] b, do #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this sbyte[,] a, int[][] b, double[][] result) + public static double[][] DotWithTransposed(this sbyte[,] a, int[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -145905,20 +147891,22 @@ public static unsafe double[][] DotWithTransposed(this sbyte[,] a, int[][] b, do #endif int n = a.Rows(); - fixed (sbyte* A = a) - for (int j = 0; j < b.Length; j++) - { - sbyte* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - int[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < b.Length; j++) + { + sbyte* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + int[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -146064,7 +148052,7 @@ public static double[][] DotWithTransposed(this sbyte[][] a, int[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this sbyte[,] a, int[,] b, double[,] result) + public static double[,] TransposeAndDot(this sbyte[,] a, int[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -146075,35 +148063,37 @@ public static double[][] DotWithTransposed(this sbyte[][] a, int[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (int* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (int* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - int* pb = B; - - for (int k = 0; k < n; k++) - { - sbyte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + int* pb = B; + + for (int k = 0; k < n; k++) + { + sbyte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -146497,18 +148487,22 @@ public static double[][] DivideByDiagonal(this sbyte[][] a, int[] diagonal, doub #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this sbyte[] a, int[] b, double[,] result) + public static double[,] Outer(this sbyte[] a, int[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -146671,25 +148665,28 @@ public static double[][] Kronecker(this sbyte[][] a, int[][] b, double[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this sbyte[][] a, int[,] b, double[][] result) + public static double[][] Kronecker(this sbyte[][] a, int[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (int* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - int* pb = B; + unsafe + { + fixed (int* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + int* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -146743,18 +148740,22 @@ public static double[][] Kronecker(this sbyte[,] a, int[][] b, double[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this sbyte[] a, int[] b, double[] result) + public static double[] Kronecker(this sbyte[] a, int[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -146948,7 +148949,7 @@ public static sbyte[][] Dot(this sbyte[][] a, float[,] b, sbyte[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] Dot(this sbyte[,] a, float[][] b, sbyte[][] result) + public static sbyte[][] Dot(this sbyte[,] a, float[][] b, sbyte[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -146962,21 +148963,24 @@ public static unsafe sbyte[][] Dot(this sbyte[,] a, float[][] b, sbyte[][] resul var t = new float[K]; - fixed (sbyte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - sbyte* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (sbyte)s; - } - } + sbyte* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (sbyte)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -147054,7 +149058,7 @@ public static sbyte[] Dot(this sbyte[][] matrix, float[] columnVector, sbyte[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[] Dot(this sbyte[,] matrix, float[] columnVector, sbyte[] result) + public static sbyte[] Dot(this sbyte[,] matrix, float[] columnVector, sbyte[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -147063,55 +149067,54 @@ public static unsafe sbyte[] Dot(this sbyte[,] matrix, float[] columnVector, sby if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (sbyte* a = matrix) + fixed (float* x = columnVector) + fixed (sbyte* r = result) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + sbyte* pr = r; - fixed (sbyte* a = matrix) - fixed (float* x = columnVector) - fixed (sbyte* r = result) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - sbyte* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (sbyte)sum1; - *pr++ = (sbyte)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (sbyte)sum1; + *pr++ = (sbyte)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (sbyte)sum; - } - } + *pr = (sbyte)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -147217,7 +149220,7 @@ public static sbyte[] Dot(this sbyte[] rowVector, float[][] matrix, sbyte[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] DotWithTransposed(this sbyte[][] a, float[,] b, sbyte[][] result) + public static sbyte[][] DotWithTransposed(this sbyte[][] a, float[,] b, sbyte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -147227,19 +149230,22 @@ public static unsafe sbyte[][] DotWithTransposed(this sbyte[][] a, float[,] b, s #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - sbyte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (sbyte)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + sbyte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (sbyte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -147261,7 +149267,7 @@ public static unsafe sbyte[][] DotWithTransposed(this sbyte[][] a, float[,] b, s #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] DotWithTransposed(this sbyte[,] a, float[][] b, sbyte[][] result) + public static sbyte[][] DotWithTransposed(this sbyte[,] a, float[][] b, sbyte[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -147271,20 +149277,22 @@ public static unsafe sbyte[][] DotWithTransposed(this sbyte[,] a, float[][] b, s #endif int n = a.Rows(); - fixed (sbyte* A = a) - for (int j = 0; j < b.Length; j++) - { - sbyte* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (sbyte)sum; - } - } - + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < b.Length; j++) + { + sbyte* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (sbyte)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -147430,7 +149438,7 @@ public static sbyte[][] DotWithTransposed(this sbyte[][] a, float[] columnVector #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[,] TransposeAndDot(this sbyte[,] a, float[,] b, sbyte[,] result) + public static sbyte[,] TransposeAndDot(this sbyte[,] a, float[,] b, sbyte[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -147441,35 +149449,37 @@ public static sbyte[][] DotWithTransposed(this sbyte[][] a, float[] columnVector throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (sbyte* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + sbyte* pr = R; - fixed (sbyte* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - sbyte* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - sbyte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (sbyte)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + sbyte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (sbyte)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -147863,18 +149873,22 @@ public static sbyte[][] DivideByDiagonal(this sbyte[][] a, float[] diagonal, sby #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[,] Outer(this sbyte[] a, float[] b, sbyte[,] result) + public static sbyte[,] Outer(this sbyte[] a, float[] b, sbyte[,] result) { - fixed (sbyte* R = result) - { - sbyte* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (sbyte)(x * (double)b[j]); - } - } + unsafe + { + fixed (sbyte* R = result) + { + sbyte* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (sbyte)(x * (double)b[j]); + } + } + } + return result; } @@ -148037,25 +150051,28 @@ public static sbyte[][] Kronecker(this sbyte[][] a, float[][] b, sbyte[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[][] Kronecker(this sbyte[][] a, float[,] b, sbyte[][] result) + public static sbyte[][] Kronecker(this sbyte[][] a, float[,] b, sbyte[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (sbyte)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (sbyte)(aval * (double)(*pb++)); + } + } + return result; } @@ -148109,18 +150126,22 @@ public static sbyte[][] Kronecker(this sbyte[,] a, float[][] b, sbyte[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe sbyte[] Kronecker(this sbyte[] a, float[] b, sbyte[] result) + public static sbyte[] Kronecker(this sbyte[] a, float[] b, sbyte[] result) { - fixed (sbyte* R = result) - { - sbyte* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (sbyte)(x * (double)b[j]); - } - } + unsafe + { + fixed (sbyte* R = result) + { + sbyte* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (sbyte)(x * (double)b[j]); + } + } + } + return result; } @@ -148314,7 +150335,7 @@ public static float[][] Dot(this sbyte[][] a, float[,] b, float[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Dot(this sbyte[,] a, float[][] b, float[][] result) + public static float[][] Dot(this sbyte[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -148328,21 +150349,24 @@ public static unsafe float[][] Dot(this sbyte[,] a, float[][] b, float[][] resul var t = new float[K]; - fixed (sbyte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - sbyte* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (float)s; - } - } + sbyte* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (float)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -148420,7 +150444,7 @@ public static float[] Dot(this sbyte[][] matrix, float[] columnVector, float[] r #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Dot(this sbyte[,] matrix, float[] columnVector, float[] result) + public static float[] Dot(this sbyte[,] matrix, float[] columnVector, float[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -148429,55 +150453,54 @@ public static unsafe float[] Dot(this sbyte[,] matrix, float[] columnVector, flo if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (sbyte* a = matrix) + fixed (float* x = columnVector) + fixed (float* r = result) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + float* pr = r; - fixed (sbyte* a = matrix) - fixed (float* x = columnVector) - fixed (float* r = result) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - float* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (float)sum1; - *pr++ = (float)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (float)sum1; + *pr++ = (float)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (float)sum; - } - } + *pr = (float)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -148583,7 +150606,7 @@ public static float[] Dot(this sbyte[] rowVector, float[][] matrix, float[] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this sbyte[][] a, float[,] b, float[][] result) + public static float[][] DotWithTransposed(this sbyte[][] a, float[,] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -148593,19 +150616,22 @@ public static unsafe float[][] DotWithTransposed(this sbyte[][] a, float[,] b, f #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - sbyte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (float)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + sbyte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -148627,7 +150653,7 @@ public static unsafe float[][] DotWithTransposed(this sbyte[][] a, float[,] b, f #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] DotWithTransposed(this sbyte[,] a, float[][] b, float[][] result) + public static float[][] DotWithTransposed(this sbyte[,] a, float[][] b, float[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -148637,20 +150663,22 @@ public static unsafe float[][] DotWithTransposed(this sbyte[,] a, float[][] b, f #endif int n = a.Rows(); - fixed (sbyte* A = a) - for (int j = 0; j < b.Length; j++) - { - sbyte* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (float)sum; - } - } - + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < b.Length; j++) + { + sbyte* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (float)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -148796,7 +150824,7 @@ public static float[][] DotWithTransposed(this sbyte[][] a, float[] columnVector #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] TransposeAndDot(this sbyte[,] a, float[,] b, float[,] result) + public static float[,] TransposeAndDot(this sbyte[,] a, float[,] b, float[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -148807,35 +150835,37 @@ public static float[][] DotWithTransposed(this sbyte[][] a, float[] columnVector throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (float* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + float* pr = R; - fixed (float* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - float* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - sbyte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (float)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + sbyte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (float)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -149229,18 +151259,22 @@ public static float[][] DivideByDiagonal(this sbyte[][] a, float[] diagonal, flo #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[,] Outer(this sbyte[] a, float[] b, float[,] result) + public static float[,] Outer(this sbyte[] a, float[] b, float[,] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -149403,25 +151437,28 @@ public static float[][] Kronecker(this sbyte[][] a, float[][] b, float[][] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[][] Kronecker(this sbyte[][] a, float[,] b, float[][] result) + public static float[][] Kronecker(this sbyte[][] a, float[,] b, float[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (float)(aval * (double)(*pb++)); + } + } + return result; } @@ -149475,18 +151512,22 @@ public static float[][] Kronecker(this sbyte[,] a, float[][] b, float[][] result #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe float[] Kronecker(this sbyte[] a, float[] b, float[] result) + public static float[] Kronecker(this sbyte[] a, float[] b, float[] result) { - fixed (float* R = result) - { - float* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (float)(x * (double)b[j]); - } - } + unsafe + { + fixed (float* R = result) + { + float* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (float)(x * (double)b[j]); + } + } + } + return result; } @@ -149680,7 +151721,7 @@ public static double[][] Dot(this sbyte[][] a, float[,] b, double[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Dot(this sbyte[,] a, float[][] b, double[][] result) + public static double[][] Dot(this sbyte[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -149694,21 +151735,24 @@ public static unsafe double[][] Dot(this sbyte[,] a, float[][] b, double[][] res var t = new float[K]; - fixed (sbyte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - sbyte* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (double)s; - } - } + sbyte* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (double)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -149786,7 +151830,7 @@ public static double[] Dot(this sbyte[][] matrix, float[] columnVector, double[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Dot(this sbyte[,] matrix, float[] columnVector, double[] result) + public static double[] Dot(this sbyte[,] matrix, float[] columnVector, double[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -149795,55 +151839,54 @@ public static unsafe double[] Dot(this sbyte[,] matrix, float[] columnVector, do if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (sbyte* a = matrix) + fixed (float* x = columnVector) + fixed (double* r = result) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + double* pr = r; - fixed (sbyte* a = matrix) - fixed (float* x = columnVector) - fixed (double* r = result) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - double* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (double)sum1; - *pr++ = (double)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (double)sum1; + *pr++ = (double)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (double)sum; - } - } + *pr = (double)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -149949,7 +151992,7 @@ public static double[] Dot(this sbyte[] rowVector, float[][] matrix, double[] re #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this sbyte[][] a, float[,] b, double[][] result) + public static double[][] DotWithTransposed(this sbyte[][] a, float[,] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -149959,19 +152002,22 @@ public static unsafe double[][] DotWithTransposed(this sbyte[][] a, float[,] b, #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - sbyte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (double)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + sbyte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -149993,7 +152039,7 @@ public static unsafe double[][] DotWithTransposed(this sbyte[][] a, float[,] b, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] DotWithTransposed(this sbyte[,] a, float[][] b, double[][] result) + public static double[][] DotWithTransposed(this sbyte[,] a, float[][] b, double[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -150003,20 +152049,22 @@ public static unsafe double[][] DotWithTransposed(this sbyte[,] a, float[][] b, #endif int n = a.Rows(); - fixed (sbyte* A = a) - for (int j = 0; j < b.Length; j++) - { - sbyte* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (double)sum; - } - } - + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < b.Length; j++) + { + sbyte* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (double)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -150162,7 +152210,7 @@ public static double[][] DotWithTransposed(this sbyte[][] a, float[] columnVecto #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] TransposeAndDot(this sbyte[,] a, float[,] b, double[,] result) + public static double[,] TransposeAndDot(this sbyte[,] a, float[,] b, double[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -150173,35 +152221,37 @@ public static double[][] DotWithTransposed(this sbyte[][] a, float[] columnVecto throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (double* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + double* pr = R; - fixed (double* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - double* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - sbyte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (double)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + sbyte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (double)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -150595,18 +152645,22 @@ public static double[][] DivideByDiagonal(this sbyte[][] a, float[] diagonal, do #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[,] Outer(this sbyte[] a, float[] b, double[,] result) + public static double[,] Outer(this sbyte[] a, float[] b, double[,] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -150769,25 +152823,28 @@ public static double[][] Kronecker(this sbyte[][] a, float[][] b, double[][] res #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[][] Kronecker(this sbyte[][] a, float[,] b, double[][] result) + public static double[][] Kronecker(this sbyte[][] a, float[,] b, double[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (double)(aval * (double)(*pb++)); + } + } + return result; } @@ -150841,18 +152898,22 @@ public static double[][] Kronecker(this sbyte[,] a, float[][] b, double[][] resu #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe double[] Kronecker(this sbyte[] a, float[] b, double[] result) + public static double[] Kronecker(this sbyte[] a, float[] b, double[] result) { - fixed (double* R = result) - { - double* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (double)(x * (double)b[j]); - } - } + unsafe + { + fixed (double* R = result) + { + double* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (double)(x * (double)b[j]); + } + } + } + return result; } @@ -151046,7 +153107,7 @@ public static int[][] Dot(this sbyte[][] a, float[,] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Dot(this sbyte[,] a, float[][] b, int[][] result) + public static int[][] Dot(this sbyte[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -151060,21 +153121,24 @@ public static unsafe int[][] Dot(this sbyte[,] a, float[][] b, int[][] result) var t = new float[K]; - fixed (sbyte* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - sbyte* pa = A; - for (int i = 0; i < N; i++) - { - double s = (double)0; - for (int k = 0; k < t.Length; k++) - s += (double)((double)(*pa++) * (double)t[k]); - result[i][j] = (int)s; - } - } + sbyte* pa = A; + for (int i = 0; i < N; i++) + { + double s = (double)0; + for (int k = 0; k < t.Length; k++) + s += (double)((double)(*pa++) * (double)t[k]); + result[i][j] = (int)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -151152,7 +153216,7 @@ public static int[] Dot(this sbyte[][] matrix, float[] columnVector, int[] resul #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Dot(this sbyte[,] matrix, float[] columnVector, int[] result) + public static int[] Dot(this sbyte[,] matrix, float[] columnVector, int[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -151161,55 +153225,54 @@ public static unsafe int[] Dot(this sbyte[,] matrix, float[] columnVector, int[] if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (sbyte* a = matrix) + fixed (float* x = columnVector) + fixed (int* r = result) + { + sbyte* pa1 = a; + sbyte* pa2 = a + cols; + int* pr = r; - fixed (sbyte* a = matrix) - fixed (float* x = columnVector) - fixed (int* r = result) - { - sbyte* pa1 = a; - sbyte* pa2 = a + cols; - int* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - double sum1 = 0, sum2 = 0; - float* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (double)((double)(*pa1++) * (double)(*px)); - sum2 += (double)((double)(*pa2++) * (double)(*px)); - px++; - } - - *pr++ = (int)sum1; - *pr++ = (int)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + double sum1 = 0, sum2 = 0; + float* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (double)((double)(*pa1++) * (double)(*px)); + sum2 += (double)((double)(*pa2++) * (double)(*px)); + px++; + } + + *pr++ = (int)sum1; + *pr++ = (int)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - double sum = 0; - float* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + double sum = 0; + float* px = x; - for (int j = 0; j < cols; j++) - sum += (double)((double)(*pa1++) * (double)(*px++)); + for (int j = 0; j < cols; j++) + sum += (double)((double)(*pa1++) * (double)(*px++)); - *pr = (int)sum; - } - } + *pr = (int)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -151315,7 +153378,7 @@ public static int[] Dot(this sbyte[] rowVector, float[][] matrix, int[] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this sbyte[][] a, float[,] b, int[][] result) + public static int[][] DotWithTransposed(this sbyte[][] a, float[,] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -151325,19 +153388,22 @@ public static unsafe int[][] DotWithTransposed(this sbyte[][] a, float[,] b, int #endif int n = b.Rows(); - fixed (float* B = b) - for (int i = 0; i < a.Length; i++) - { - float* pb = B; - sbyte[] arow = a[i]; - for (int j = 0; j < n; j++) - { - double sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (double)((double)arow[k] * (double)(*pb++)); - result[i][j] = (int)sum; - } - } + unsafe + { + fixed (float* B = b) + for (int i = 0; i < a.Length; i++) + { + float* pb = B; + sbyte[] arow = a[i]; + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (double)((double)arow[k] * (double)(*pb++)); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -151359,7 +153425,7 @@ public static unsafe int[][] DotWithTransposed(this sbyte[][] a, float[,] b, int #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] DotWithTransposed(this sbyte[,] a, float[][] b, int[][] result) + public static int[][] DotWithTransposed(this sbyte[,] a, float[][] b, int[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -151369,20 +153435,22 @@ public static unsafe int[][] DotWithTransposed(this sbyte[,] a, float[][] b, int #endif int n = a.Rows(); - fixed (sbyte* A = a) - for (int j = 0; j < b.Length; j++) - { - sbyte* pa = A; - for (int i = 0; i < n; i++) - { - double sum = 0; - float[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (double)((double)(*pa++) * (double)brow[k]); - result[i][j] = (int)sum; - } - } - + unsafe + { + fixed (sbyte* A = a) + for (int j = 0; j < b.Length; j++) + { + sbyte* pa = A; + for (int i = 0; i < n; i++) + { + double sum = 0; + float[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (double)((double)(*pa++) * (double)brow[k]); + result[i][j] = (int)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -151528,7 +153596,7 @@ public static int[][] DotWithTransposed(this sbyte[][] a, float[] columnVector, #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] TransposeAndDot(this sbyte[,] a, float[,] b, int[,] result) + public static int[,] TransposeAndDot(this sbyte[,] a, float[,] b, int[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -151539,35 +153607,37 @@ public static int[][] DotWithTransposed(this sbyte[][] a, float[] columnVector, throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (int* R = result) + fixed (float* B = b) + fixed (double* ptemp = new double[p]) + { + int* pr = R; - fixed (int* R = result) - fixed (float* B = b) - fixed (double* ptemp = new double[p]) - { - int* pr = R; - - for (int i = 0; i < m; i++) - { - double* pt = ptemp; - float* pb = B; - - for (int k = 0; k < n; k++) - { - sbyte aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (double)((double)aval * (double)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (int)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + double* pt = ptemp; + float* pb = B; + + for (int k = 0; k < n; k++) + { + sbyte aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (double)((double)aval * (double)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (int)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -151961,18 +154031,22 @@ public static int[][] DivideByDiagonal(this sbyte[][] a, float[] diagonal, int[] #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[,] Outer(this sbyte[] a, float[] b, int[,] result) + public static int[,] Outer(this sbyte[] a, float[] b, int[,] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } @@ -152135,25 +154209,28 @@ public static int[][] Kronecker(this sbyte[][] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[][] Kronecker(this sbyte[][] a, float[,] b, int[][] result) + public static int[][] Kronecker(this sbyte[][] a, float[,] b, int[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (float* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - double aval = (double)a[i][j]; - float* pb = B; + unsafe + { + fixed (float* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + double aval = (double)a[i][j]; + float* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (int)(aval * (double)(*pb++)); + } + } + return result; } @@ -152207,18 +154284,22 @@ public static int[][] Kronecker(this sbyte[,] a, float[][] b, int[][] result) #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe int[] Kronecker(this sbyte[] a, float[] b, int[] result) + public static int[] Kronecker(this sbyte[] a, float[] b, int[] result) { - fixed (int* R = result) - { - int* pr = R; - for (int i = 0; i < a.Length; i++) - { - double x = (double)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (int)(x * (double)b[j]); - } - } + unsafe + { + fixed (int* R = result) + { + int* pr = R; + for (int i = 0; i < a.Length; i++) + { + double x = (double)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (int)(x * (double)b[j]); + } + } + } + return result; } diff --git a/Sources/Accord.Math/Matrix/Matrix.Product.tt b/Sources/Accord.Math/Matrix/Matrix.Product.tt index cc7cad4fc..eb481b824 100644 --- a/Sources/Accord.Math/Matrix/Matrix.Product.tt +++ b/Sources/Accord.Math/Matrix/Matrix.Product.tt @@ -854,7 +854,7 @@ namespace Accord.Math #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe <#=t#> DotAndDot(this <#=a#>[] rowVector, <#=b#>[,] matrix, <#=c#>[] columnVector) + public static <#=t#> DotAndDot(this <#=a#>[] rowVector, <#=b#>[,] matrix, <#=c#>[] columnVector) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -864,48 +864,51 @@ namespace Accord.Math #endif <#=t#> result = 0; - fixed (<#=a#>* r = rowVector) - fixed (<#=b#>* a = matrix) - fixed (<#=c#>* c = columnVector) - { - <#=b#>* pa1 = a; - <#=b#>* pa2 = a + cols; - <#=a#>* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - <#=t#> sum1 = 0, sum2 = 0; - <#=c#>* pc = c; - - for (int j = 0; j < cols; j++) - { - sum1 += (<#=t#>)((<#=t#>)(*pa1++) * (<#=t#>)(*pc)); - sum2 += (<#=t#>)((<#=t#>)(*pa2++) * (<#=t#>)(*pc)); - pc++; - } - - result += (<#=t#>)((<#=t#>)(*pr++) * sum1); - result += (<#=t#>)((<#=t#>)(*pr++) * sum2); - - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + unsafe + { + fixed (<#=a#>* r = rowVector) + fixed (<#=b#>* a = matrix) + fixed (<#=c#>* c = columnVector) + { + <#=b#>* pa1 = a; + <#=b#>* pa2 = a + cols; + <#=a#>* pr = r; - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - <#=t#> sum = 0; - <#=c#>* pc = c; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + <#=t#> sum1 = 0, sum2 = 0; + <#=c#>* pc = c; + + for (int j = 0; j < cols; j++) + { + sum1 += (<#=t#>)((<#=t#>)(*pa1++) * (<#=t#>)(*pc)); + sum2 += (<#=t#>)((<#=t#>)(*pa2++) * (<#=t#>)(*pc)); + pc++; + } + + result += (<#=t#>)((<#=t#>)(*pr++) * sum1); + result += (<#=t#>)((<#=t#>)(*pr++) * sum2); + + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - for (int j = 0; j < cols; j++) - sum += (<#=t#>)((<#=t#>)(*pa1++) * (<#=t#>)(*pc++)); + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + <#=t#> sum = 0; + <#=c#>* pc = c; - result += (<#=t#>)((<#=t#>)(*pr++) * sum); - } - } + for (int j = 0; j < cols; j++) + sum += (<#=t#>)((<#=t#>)(*pa1++) * (<#=t#>)(*pc++)); + result += (<#=t#>)((<#=t#>)(*pr++) * sum); + } + } + } + return result; } @@ -1112,7 +1115,7 @@ namespace Accord.Math #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe <#=r#>[][] Dot(this <#=a#>[,] a, <#=b#>[][] b, <#=r#>[][] result) + public static <#=r#>[][] Dot(this <#=a#>[,] a, <#=b#>[][] b, <#=r#>[][] result) { #if DEBUG if (a.Columns() != b.Length || result.Length > a.Length || result.Columns() > b.Columns()) @@ -1126,21 +1129,24 @@ namespace Accord.Math var t = new <#=b#>[K]; - fixed (<#=a#>* A = a) - for (int j = 0; j < M; j++) - { - for (int k = 0; k < t.Length; k++) - t[k] = b[k][j]; + unsafe + { + fixed (<#=a#>* A = a) + for (int j = 0; j < M; j++) + { + for (int k = 0; k < t.Length; k++) + t[k] = b[k][j]; - <#=a#>* pa = A; - for (int i = 0; i < N; i++) - { - <#=t#> s = (<#=t#>)0; - for (int k = 0; k < t.Length; k++) - s += (<#=t#>)((<#=t#>)(*pa++) * (<#=t#>)t[k]); - result[i][j] = (<#=r#>)s; - } - } + <#=a#>* pa = A; + for (int i = 0; i < N; i++) + { + <#=t#> s = (<#=t#>)0; + for (int k = 0; k < t.Length; k++) + s += (<#=t#>)((<#=t#>)(*pa++) * (<#=t#>)t[k]); + result[i][j] = (<#=r#>)s; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.ToMatrix().To(), 1e-4)) throw new Exception(); @@ -1219,7 +1225,7 @@ namespace Accord.Math #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe <#=r#>[] Dot(this <#=a#>[,] matrix, <#=b#>[] columnVector, <#=r#>[] result) + public static <#=r#>[] Dot(this <#=a#>[,] matrix, <#=b#>[] columnVector, <#=r#>[] result) { int cols = matrix.Columns(); int rows = matrix.Rows(); @@ -1228,55 +1234,54 @@ namespace Accord.Math if (cols != columnVector.Length || result.Length > rows) throw new DimensionMismatchException(); #endif + unsafe + { + fixed (<#=a#>* a = matrix) + fixed (<#=b#>* x = columnVector) + fixed (<#=r#>* r = result) + { + <#=a#>* pa1 = a; + <#=a#>* pa2 = a + cols; + <#=r#>* pr = r; - fixed (<#=a#>* a = matrix) - fixed (<#=b#>* x = columnVector) - fixed (<#=r#>* r = result) - { - <#=a#>* pa1 = a; - <#=a#>* pa2 = a + cols; - <#=r#>* pr = r; - - // Process rows two at a time - for (int i = 0; i < rows / 2; i++) - { - <#=t#> sum1 = 0, sum2 = 0; - <#=b#>* px = x; - - for (int j = 0; j < cols; j++) - { - sum1 += (<#=t#>)((<#=t#>)(*pa1++) * (<#=t#>)(*px)); - sum2 += (<#=t#>)((<#=t#>)(*pa2++) * (<#=t#>)(*px)); - px++; - } - - *pr++ = (<#=r#>)sum1; - *pr++ = (<#=r#>)sum2; + // Process rows two at a time + for (int i = 0; i < rows / 2; i++) + { + <#=t#> sum1 = 0, sum2 = 0; + <#=b#>* px = x; + + for (int j = 0; j < cols; j++) + { + sum1 += (<#=t#>)((<#=t#>)(*pa1++) * (<#=t#>)(*px)); + sum2 += (<#=t#>)((<#=t#>)(*pa2++) * (<#=t#>)(*px)); + px++; + } + + *pr++ = (<#=r#>)sum1; + *pr++ = (<#=r#>)sum2; - // Now we skip a row - pa1 = pa2; - pa2 += cols; - } + // Now we skip a row + pa1 = pa2; + pa2 += cols; + } - // Process the remainder - for (int i = 0; i < rows % 2; i++) - { - <#=t#> sum = 0; - <#=b#>* px = x; + // Process the remainder + for (int i = 0; i < rows % 2; i++) + { + <#=t#> sum = 0; + <#=b#>* px = x; - for (int j = 0; j < cols; j++) - sum += (<#=t#>)((<#=t#>)(*pa1++) * (<#=t#>)(*px++)); + for (int j = 0; j < cols; j++) + sum += (<#=t#>)((<#=t#>)(*pa1++) * (<#=t#>)(*px++)); - *pr = (<#=r#>)sum; - } - } + *pr = (<#=r#>)sum; + } + } + } return result; } - - - /// /// Computes the product R = A*B of two matrices A /// and B, storing the result in matrix R. @@ -1382,7 +1387,7 @@ namespace Accord.Math #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe <#=r#>[][] DotWithTransposed(this <#=a#>[][] a, <#=b#>[,] b, <#=r#>[][] result) + public static <#=r#>[][] DotWithTransposed(this <#=a#>[][] a, <#=b#>[,] b, <#=r#>[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -1392,19 +1397,22 @@ namespace Accord.Math #endif int n = b.Rows(); - fixed (<#=b#>* B = b) - for (int i = 0; i < a.Length; i++) - { - <#=b#>* pb = B; - <#=a#>[] arow = a[i]; - for (int j = 0; j < n; j++) - { - <#=t#> sum = 0; - for (int k = 0; k < arow.Length; k++) - sum += (<#=t#>)((<#=t#>)arow[k] * (<#=t#>)(*pb++)); - result[i][j] = (<#=r#>)sum; - } - } + unsafe + { + fixed (<#=b#>* B = b) + for (int i = 0; i < a.Length; i++) + { + <#=b#>* pb = B; + <#=a#>[] arow = a[i]; + for (int j = 0; j < n; j++) + { + <#=t#> sum = 0; + for (int k = 0; k < arow.Length; k++) + sum += (<#=t#>)((<#=t#>)arow[k] * (<#=t#>)(*pb++)); + result[i][j] = (<#=r#>)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -1426,7 +1434,7 @@ namespace Accord.Math #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe <#=r#>[][] DotWithTransposed(this <#=a#>[,] a, <#=b#>[][] b, <#=r#>[][] result) + public static <#=r#>[][] DotWithTransposed(this <#=a#>[,] a, <#=b#>[][] b, <#=r#>[][] result) { #if DEBUG if (a.Columns() != b.Columns() || result.Rows() > a.Rows() || result.Columns() > b.Rows()) @@ -1436,20 +1444,22 @@ namespace Accord.Math #endif int n = a.Rows(); - fixed (<#=a#>* A = a) - for (int j = 0; j < b.Length; j++) - { - <#=a#>* pa = A; - for (int i = 0; i < n; i++) - { - <#=t#> sum = 0; - <#=b#>[] brow = b[j]; - for (int k = 0; k < brow.Length; k++) - sum += (<#=t#>)((<#=t#>)(*pa++) * (<#=t#>)brow[k]); - result[i][j] = (<#=r#>)sum; - } - } - + unsafe + { + fixed (<#=a#>* A = a) + for (int j = 0; j < b.Length; j++) + { + <#=a#>* pa = A; + for (int i = 0; i < n; i++) + { + <#=t#> sum = 0; + <#=b#>[] brow = b[j]; + for (int k = 0; k < brow.Length; k++) + sum += (<#=t#>)((<#=t#>)(*pa++) * (<#=t#>)brow[k]); + result[i][j] = (<#=r#>)sum; + } + } + } #if DEBUG if (!Matrix.IsEqual(C, result.To(), 1e-4)) throw new Exception(); @@ -1599,7 +1609,7 @@ namespace Accord.Math #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe <#=r#>[,] TransposeAndDot(this <#=a#>[,] a, <#=b#>[,] b, <#=r#>[,] result) + public static <#=r#>[,] TransposeAndDot(this <#=a#>[,] a, <#=b#>[,] b, <#=r#>[,] result) { int n = a.Rows(); int m = a.Columns(); @@ -1610,35 +1620,37 @@ namespace Accord.Math throw new DimensionMismatchException(); var C = a.Transpose().To().Dot(b.To()); #endif + unsafe + { + fixed (<#=r#>* R = result) + fixed (<#=b#>* B = b) + fixed (<#=t#>* ptemp = new <#=t#>[p]) + { + <#=r#>* pr = R; - fixed (<#=r#>* R = result) - fixed (<#=b#>* B = b) - fixed (<#=t#>* ptemp = new <#=t#>[p]) - { - <#=r#>* pr = R; - - for (int i = 0; i < m; i++) - { - <#=t#>* pt = ptemp; - <#=b#>* pb = B; - - for (int k = 0; k < n; k++) - { - <#=a#> aval = a[k, i]; - for (int j = 0; j < p; j++) - *pt++ += (<#=t#>)((<#=t#>)aval * (<#=t#>)(*pb++)); - pt = ptemp; - } - - // Update the results row and clear the cache - for (int j = 0; j < p; j++) - { - *pr++ = (<#=r#>)*pt; - *pt++ = 0; - } - } - } - + for (int i = 0; i < m; i++) + { + <#=t#>* pt = ptemp; + <#=b#>* pb = B; + + for (int k = 0; k < n; k++) + { + <#=a#> aval = a[k, i]; + for (int j = 0; j < p; j++) + *pt++ += (<#=t#>)((<#=t#>)aval * (<#=t#>)(*pb++)); + pt = ptemp; + } + + // Update the results row and clear the cache + for (int j = 0; j < p; j++) + { + *pr++ = (<#=r#>)*pt; + *pt++ = 0; + } + } + } + } + #if DEBUG if (!C.IsEqual(result.To(), 1e-4)) throw new Exception(); @@ -2037,18 +2049,22 @@ namespace Accord.Math #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe <#=r#>[,] Outer(this <#=a#>[] a, <#=b#>[] b, <#=r#>[,] result) + public static <#=r#>[,] Outer(this <#=a#>[] a, <#=b#>[] b, <#=r#>[,] result) { - fixed (<#=r#>* R = result) - { - <#=r#>* pr = R; - for (int i = 0; i < a.Length; i++) - { - <#=t#> x = (<#=t#>)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (<#=r#>)(x * (<#=t#>)b[j]); - } - } + unsafe + { + fixed (<#=r#>* R = result) + { + <#=r#>* pr = R; + for (int i = 0; i < a.Length; i++) + { + <#=t#> x = (<#=t#>)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (<#=r#>)(x * (<#=t#>)b[j]); + } + } + } + return result; } @@ -2211,25 +2227,28 @@ namespace Accord.Math #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe <#=r#>[][] Kronecker(this <#=a#>[][] a, <#=b#>[,] b, <#=r#>[][] result) + public static <#=r#>[][] Kronecker(this <#=a#>[][] a, <#=b#>[,] b, <#=r#>[][] result) { int arows = a.Rows(); int acols = a.Columns(); int brows = b.Rows(); int bcols = b.Columns(); - fixed (<#=b#>* B = b) - for (int i = 0; i < arows; i++) - for (int j = 0; j < acols; j++) - { - <#=t#> aval = (<#=t#>)a[i][j]; - <#=b#>* pb = B; + unsafe + { + fixed (<#=b#>* B = b) + for (int i = 0; i < arows; i++) + for (int j = 0; j < acols; j++) + { + <#=t#> aval = (<#=t#>)a[i][j]; + <#=b#>* pb = B; - for (int k = 0; k < brows; k++) - for (int l = 0; l < bcols; l++) - result[i * brows + k][j * bcols + l] = (<#=r#>)(aval * (<#=t#>)(*pb++)); - } - + for (int k = 0; k < brows; k++) + for (int l = 0; l < bcols; l++) + result[i * brows + k][j * bcols + l] = (<#=r#>)(aval * (<#=t#>)(*pb++)); + } + } + return result; } @@ -2283,18 +2302,22 @@ namespace Accord.Math #if NET45 || NET46 || NET462 || NETSTANDARD [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static unsafe <#=r#>[] Kronecker(this <#=a#>[] a, <#=b#>[] b, <#=r#>[] result) + public static <#=r#>[] Kronecker(this <#=a#>[] a, <#=b#>[] b, <#=r#>[] result) { - fixed (<#=r#>* R = result) - { - <#=r#>* pr = R; - for (int i = 0; i < a.Length; i++) - { - <#=t#> x = (<#=t#>)a[i]; - for (int j = 0; j < b.Length; j++) - *pr++ = (<#=r#>)(x * (<#=t#>)b[j]); - } - } + unsafe + { + fixed (<#=r#>* R = result) + { + <#=r#>* pr = R; + for (int i = 0; i < a.Length; i++) + { + <#=t#> x = (<#=t#>)a[i]; + for (int j = 0; j < b.Length; j++) + *pr++ = (<#=r#>)(x * (<#=t#>)b[j]); + } + } + } + return result; }