mirror of
https://github.com/hanzoai/jube-fork.git
synced 2026-10-30 18:33:33 +00:00
Included open-source Accord.net library code natively to the solution given the deprecated state of that project. Accord.net will be supported inside Jube for own needs (such as upgrading to .Net 7+). Accord.net libraries modified to compile to .Net 6. Modified Accord.net libraries to better support Newtonsoft Json.net serialisation. Included annotations in the Accord.net libraries to house important dependency statistics. Serialised json can be used independently of Jube to recall a Neural Network. Removal of Binary Serializer totally as a precursor to the projects support of .Net 7+. Extensive bug fixing in crucial functions of Neural Network training and recall. While this part of the system remains in Alpha, it is substantially more fit for purpose.
463 lines
15 KiB
Plaintext
463 lines
15 KiB
Plaintext
<#@ template debug="false" hostspecific="true" language="C#" #>
|
|
<#@ output extension="Generated.cs" #>
|
|
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
|
|
<#@ include file="T4Toolbox.tt" #>
|
|
// Accord Math Library
|
|
// The Accord.NET Framework
|
|
// http://accord-framework.net
|
|
//
|
|
// Copyright © César Souza, 2009-2017
|
|
// cesarsouza at gmail.com
|
|
//
|
|
// This library is free software; you can redistribute it and/or
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
// License as published by the Free Software Foundation; either
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
//
|
|
// This library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
// Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
// License along with this library; if not, write to the Free Software
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
//
|
|
|
|
// ======================================================================
|
|
// This code has been generated by a tool; do not edit manually. Instead,
|
|
// edit the T4 template Matrix.Elementwise.tt so this file can be regenerated.
|
|
// ======================================================================
|
|
|
|
namespace Accord.Math
|
|
{
|
|
using System;
|
|
using System.CodeDom.Compiler;
|
|
using Accord.Math;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
//[GeneratedCode("Accord.NET T4 Templates", "3.1")]
|
|
public static partial class Matrix
|
|
{
|
|
<#
|
|
string[] types =
|
|
{
|
|
"int", "short",
|
|
"float", "double",
|
|
"long", "decimal",
|
|
"bool"
|
|
//"byte", "sbyte",
|
|
};
|
|
|
|
var operations = new string[][]
|
|
{
|
|
new string[] { "Sum", "sum", "+", "0" },
|
|
new string[] { "Product", "product", "*", "1" },
|
|
};
|
|
|
|
foreach (string a in types)
|
|
{
|
|
foreach (string[] opt in operations)
|
|
{
|
|
string method = opt[0];
|
|
string name = opt[1];
|
|
string op = opt[2];
|
|
string start = opt[3];
|
|
string r = a;
|
|
string before = "(" + r + ")";
|
|
string after = "";
|
|
|
|
if (a == "bool")
|
|
{
|
|
r = "int";
|
|
before = "(";
|
|
after = " ? 1 : 0)";
|
|
}
|
|
|
|
string cast = r;
|
|
#>
|
|
|
|
/// <summary>
|
|
/// Vector <#=name#>.
|
|
/// </summary>
|
|
///
|
|
/// <param name="vector">A vector whose <#=name#> will be calculated.</param>
|
|
///
|
|
#if NET45 || NET46 || NET462 || NETSTANDARD2_0
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
#endif
|
|
public static <#=r#> <#=method#>(this <#=a#>[] vector)
|
|
{
|
|
<#=r#> sum = <#=start#>;
|
|
for (int i = 0; i < vector.Length; i++)
|
|
sum = (<#=cast#>)(sum <#=op#> <#=before#>vector[i]<#=after#>);
|
|
return sum;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Matrix <#=name#>.
|
|
/// </summary>
|
|
///
|
|
/// <param name="matrix">A matrix whose <#=name#> will be calculated.</param>
|
|
///
|
|
#if NET45 || NET46 || NET462 || NETSTANDARD2_0
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
#endif
|
|
public static <#=r#> <#=method#>(this <#=a#>[,] matrix)
|
|
{
|
|
<#=r#> sum = <#=start#>;
|
|
foreach (var v in matrix)
|
|
sum = (<#=cast#>)(sum <#=op#> <#=before#>v<#=after#>);
|
|
return sum;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Matrix sum.
|
|
/// </summary>
|
|
///
|
|
/// <param name="matrix">A matrix whose sums will be calculated.</param>
|
|
///
|
|
#if NET45 || NET46 || NET462 || NETSTANDARD2_0
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
#endif
|
|
public static <#=r#> <#=method#>(this <#=a#>[][] matrix)
|
|
{
|
|
<#=r#> sum = <#=start#>;
|
|
for (int i = 0; i < matrix.Length; i++)
|
|
for (int j = 0; j < matrix[i].Length; j++)
|
|
sum = (<#=cast#>)(sum <#=op#> <#=before#>matrix[i][j]<#=after#>);
|
|
return sum;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Matrix <#=name#>.
|
|
/// </summary>
|
|
///
|
|
/// <param name="matrix">A matrix whose <#=name#> will be calculated.</param>
|
|
/// <param name="dimension">The dimension in which the <#=name#> will be
|
|
/// calculated.</param>
|
|
///
|
|
#if NET45 || NET46 || NET462 || NETSTANDARD2_0
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
#endif
|
|
public static <#=r#>[] <#=method#>(this <#=a#>[][] matrix, int dimension)
|
|
{
|
|
var result = new <#=r#>[Matrix.GetLength(matrix, dimension)];
|
|
return <#=method#>(matrix, dimension, result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Matrix <#=name#>.
|
|
/// </summary>
|
|
///
|
|
/// <param name="matrix">A matrix whose <#=name#> will be calculated.</param>
|
|
/// <param name="dimension">The dimension in which the <#=name#> will be
|
|
/// calculated.</param>
|
|
///
|
|
#if NET45 || NET46 || NET462 || NETSTANDARD2_0
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
#endif
|
|
public static <#=r#>[] <#=method#>(this <#=a#>[,] matrix, int dimension)
|
|
{
|
|
var result = new <#=r#>[Matrix.GetLength(matrix, dimension)];
|
|
return <#=method#>(matrix, dimension, result);
|
|
}
|
|
|
|
<#
|
|
}
|
|
}
|
|
|
|
foreach (string a in types)
|
|
{
|
|
foreach (string r in types)
|
|
{
|
|
if (r == "bool")
|
|
continue;
|
|
|
|
foreach (string[] opt in operations)
|
|
{
|
|
string method = opt[0];
|
|
string name = opt[1];
|
|
string op = opt[2];
|
|
string start = opt[3];
|
|
|
|
string before = "(" + r + ")";
|
|
string after = "";
|
|
string cast = r;
|
|
|
|
if (a == "bool")
|
|
{
|
|
before = "(";
|
|
after = " ? 1 : 0)";
|
|
}
|
|
#>
|
|
/// <summary>
|
|
/// Matrix <#=name#>.
|
|
/// </summary>
|
|
///
|
|
/// <param name="matrix">A matrix whose <#=name#> will be calculated.</param>
|
|
/// <param name="dimension">The dimension in which the <#=name#> will be
|
|
/// calculated.</param>
|
|
/// <param name="result">A location where the result of this operation will be stored,
|
|
/// avoiding unnecessary memory allocations.</param>
|
|
///
|
|
#if NET45 || NET46 || NET462 || NETSTANDARD2_0
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
#endif
|
|
public static <#=r#>[] <#=method#>(this <#=a#>[][] matrix, int dimension, <#=r#>[] result)
|
|
{
|
|
if (matrix == null)
|
|
throw new ArgumentNullException("matrix");
|
|
|
|
int rows = matrix.Length;
|
|
int cols = matrix[0].Length;
|
|
|
|
if (dimension == 0)
|
|
{
|
|
for (int j = 0; j < cols; j++)
|
|
{
|
|
<#=r#> s = <#=start#>;
|
|
for (int i = 0; i < rows; i++)
|
|
s = (<#=cast#>)(s <#=op#> <#=before#>matrix[i][j]<#=after#>);
|
|
result[j] = s;
|
|
}
|
|
}
|
|
else if (dimension == 1)
|
|
{
|
|
for (int j = 0; j < rows; j++)
|
|
{
|
|
<#=r#> s = <#=start#>;
|
|
for (int i = 0; i < cols; i++)
|
|
s = (<#=cast#>)(s <#=op#> <#=before#>matrix[j][i]<#=after#>);
|
|
result[j] = s;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new ArgumentException("Invalid dimension", "dimension");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Matrix <#=name#>.
|
|
/// </summary>
|
|
///
|
|
/// <param name="matrix">A matrix whose <#=name#> will be calculated.</param>
|
|
/// <param name="dimension">The dimension in which the <#=name#> will be
|
|
/// calculated.</param>
|
|
/// <param name="result">A location where the result of this operation will be stored,
|
|
/// avoiding unnecessary memory allocations.</param>
|
|
///
|
|
#if NET45 || NET46 || NET462 || NETSTANDARD2_0
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
#endif
|
|
public static <#=r#>[] <#=method#>(this <#=a#>[,] matrix, int dimension, <#=r#>[] result)
|
|
{
|
|
if (matrix == null)
|
|
throw new ArgumentNullException("matrix");
|
|
|
|
int rows = matrix.GetLength(0);
|
|
int cols = matrix.GetLength(1);
|
|
|
|
if (dimension == 0)
|
|
{
|
|
for (int j = 0; j < cols; j++)
|
|
{
|
|
<#=r#> s = <#=start#>;
|
|
for (int i = 0; i < rows; i++)
|
|
s = (<#=cast#>)(s <#=op#> <#=before#>matrix[i, j]<#=after#>);
|
|
result[j] = s;
|
|
}
|
|
}
|
|
else if (dimension == 1)
|
|
{
|
|
for (int j = 0; j < rows; j++)
|
|
{
|
|
<#=r#> s = <#=start#>;
|
|
for (int i = 0; i < cols; i++)
|
|
s = (<#=cast#>)(s <#=op#> <#=before#>matrix[j, i]<#=after#>);
|
|
result[j] = s;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new ArgumentException("Invalid dimension", "dimension");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
<#
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (string a in types)
|
|
{
|
|
if (a == "bool")
|
|
continue;
|
|
#>
|
|
|
|
/// <summary>
|
|
/// Matrix cumulative sum.
|
|
/// </summary>
|
|
///
|
|
/// <param name="vector">A vector whose cumulative sum will be calculated.</param>
|
|
///
|
|
#if NET45 || NET46 || NET462 || NETSTANDARD2_0
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
#endif
|
|
public static <#=a#>[] CumulativeSum(this <#=a#>[] vector)
|
|
{
|
|
if (vector.Length == 0)
|
|
return new <#=a#>[0];
|
|
|
|
return CumulativeSum(vector, Accord.Math.Vector.CreateAs(vector));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Matrix cumulative sum.
|
|
/// </summary>
|
|
///
|
|
/// <param name="vector">A vector whose cumulative sum will be calculated.</param>
|
|
/// <param name="result">A location where the result of this operation will be stored,
|
|
/// avoiding unnecessary memory allocations.</param>
|
|
///
|
|
#if NET45 || NET46 || NET462 || NETSTANDARD2_0
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
#endif
|
|
public static <#=a#>[] CumulativeSum(this <#=a#>[] vector, <#=a#>[] result)
|
|
{
|
|
result[0] = vector[0];
|
|
for (int i = 1; i < vector.Length; i++)
|
|
result[i] = (<#=a#>)(result[i - 1] + vector[i]);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Matrix cumulative sum.
|
|
/// </summary>
|
|
///
|
|
/// <param name="matrix">A matrix whose cumulative sum will be calculated.</param>
|
|
/// <param name="dimension">The dimension in which the cumulative will be
|
|
/// calculated.</param>
|
|
///
|
|
#if NET45 || NET46 || NET462 || NETSTANDARD2_0
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
#endif
|
|
public static <#=a#>[][] CumulativeSum(this <#=a#>[][] matrix, int dimension)
|
|
{
|
|
int rows = matrix.Rows();
|
|
int cols = matrix.Columns();
|
|
if (dimension == 1)
|
|
return CumulativeSum(matrix, dimension, Jagged.Zeros<<#=a#>>(rows, cols));
|
|
return CumulativeSum(matrix, dimension, Jagged.Zeros<<#=a#>>(cols, rows));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Matrix cumulative sum.
|
|
/// </summary>
|
|
///
|
|
/// <param name="matrix">A matrix whose cumulative sum will be calculated.</param>
|
|
/// <param name="dimension">The dimension in which the cumulative will be
|
|
/// calculated.</param>
|
|
/// <param name="result">A location where the result of this operation will be stored,
|
|
/// avoiding unnecessary memory allocations.</param>
|
|
///
|
|
#if NET45 || NET46 || NET462 || NETSTANDARD2_0
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
#endif
|
|
public static <#=a#>[][] CumulativeSum(this <#=a#>[][] matrix, int dimension, <#=a#>[][] result)
|
|
{
|
|
int rows = matrix.Rows();
|
|
int cols = matrix.Columns();
|
|
|
|
if (dimension == 1)
|
|
{
|
|
matrix.GetRow(0, result: result[0]);
|
|
for (int i = 1; i < rows; i++)
|
|
for (int j = 0; j < cols; j++)
|
|
result[i][j] = (<#=a#>)(result[i - 1][j] + matrix[i][j]);
|
|
}
|
|
else if (dimension == 0)
|
|
{
|
|
matrix.GetColumn(0, result: result[0]);
|
|
for (int i = 1; i < cols; i++)
|
|
for (int j = 0; j < rows; j++)
|
|
result[i][j] = (<#=a#>)(result[i - 1][j] + matrix[j][i]);
|
|
}
|
|
else
|
|
{
|
|
throw new ArgumentException("Invalid dimension", "dimension");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Matrix cumulative sum.
|
|
/// </summary>
|
|
///
|
|
/// <param name="matrix">A matrix whose cumulative sum will be calculated.</param>
|
|
/// <param name="dimension">The dimension in which the cumulative will be
|
|
/// calculated.</param>
|
|
///
|
|
#if NET45 || NET46 || NET462 || NETSTANDARD2_0
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
#endif
|
|
public static <#=a#>[,] CumulativeSum(this <#=a#>[,] matrix, int dimension)
|
|
{
|
|
int rows = matrix.Rows();
|
|
int cols = matrix.Columns();
|
|
if (dimension == 1)
|
|
return CumulativeSum(matrix, dimension, Matrix.Zeros<<#=a#>>(rows, cols));
|
|
return CumulativeSum(matrix, dimension, Matrix.Zeros<<#=a#>>(cols, rows));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Matrix cumulative sum.
|
|
/// </summary>
|
|
///
|
|
/// <param name="matrix">A matrix whose cumulative sum will be calculated.</param>
|
|
/// <param name="dimension">The dimension in which the cumulative will be
|
|
/// calculated.</param>
|
|
/// <param name="result">A location where the result of this operation will be stored,
|
|
/// avoiding unnecessary memory allocations.</param>
|
|
///
|
|
#if NET45 || NET46 || NET462 || NETSTANDARD2_0
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
#endif
|
|
public static <#=a#>[,] CumulativeSum(this <#=a#>[,] matrix, int dimension, <#=a#>[,] result)
|
|
{
|
|
int rows = matrix.Rows();
|
|
int cols = matrix.Columns();
|
|
|
|
if (dimension == 1)
|
|
{
|
|
result.SetColumn(0, matrix.GetRow(0));
|
|
for (int i = 1; i < rows; i++)
|
|
for (int j = 0; j < cols; j++)
|
|
result[i, j] = (<#=a#>)(result[i - 1, j] + matrix[i, j]);
|
|
}
|
|
else if (dimension == 0)
|
|
{
|
|
result.SetColumn(0, matrix.GetColumn(0));
|
|
for (int i = 1; i < cols; i++)
|
|
for (int j = 0; j < rows; j++)
|
|
result[i, j] = (<#=a#>)(result[i - 1, j] + matrix[j, i]);
|
|
}
|
|
else
|
|
{
|
|
throw new ArgumentException("Invalid dimension", "dimension");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
<#
|
|
}
|
|
#>
|
|
}
|
|
} |