Files
jube-fork/Accord.Math/Matrix/Matrix.MinMax.cs
richard-churchman cc0a2bee21 Extensive modifications to the serialisation of Neural Network models to support Newtonsoft Json.net rather than Binary Serializer.
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.
2023-12-24 10:22:22 +01:00

633 lines
19 KiB
C#

// 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
//
namespace Accord.Math
{
using System;
using Accord.Compat;
public static partial class Matrix
{
#region Matrix ArgMin/ArgMax
/// <summary>
/// Gets the index of the maximum element in a matrix.
/// </summary>
///
public static Tuple<int, int> ArgMax<T>(this T[,] matrix)
where T : IComparable<T>
{
Tuple<int, int> index;
Max(matrix, out index);
return index;
}
/// <summary>
/// Gets the index of the maximum element in a matrix across a given dimension.
/// </summary>
///
public static int[] ArgMax<T>(this T[,] matrix, int dimension)
where T : IComparable<T>
{
int s = GetLength(matrix, dimension);
var values = new T[s];
var indices = new int[s];
Max(matrix, dimension, indices, values);
return indices;
}
/// <summary>
/// Gets the index of the maximum element in a matrix across a given dimension.
/// </summary>
///
public static int[] ArgMax<T>(this T[,] matrix, int dimension, int[] result)
where T : IComparable<T>
{
int s = GetLength(matrix, dimension);
var values = new T[s];
Max(matrix, dimension, result, values);
return result;
}
/// <summary>
/// Gets the index of the minimum element in a matrix.
/// </summary>
///
public static Tuple<int, int> ArgMin<T>(this T[,] matrix)
where T : IComparable<T>
{
Tuple<int, int> index;
Min(matrix, out index);
return index;
}
/// <summary>
/// Gets the index of the minimum element in a matrix across a given dimension.
/// </summary>
///
public static int[] ArgMin<T>(this T[,] matrix, int dimension)
where T : IComparable<T>
{
int s = GetLength(matrix, dimension);
var values = new T[s];
var indices = new int[s];
Min(matrix, dimension, indices, values);
return indices;
}
/// <summary>
/// Gets the index of the minimum element in a matrix across a given dimension.
/// </summary>
///
public static int[] ArgMin<T>(this T[,] matrix, int dimension, int[] result)
where T : IComparable<T>
{
int s = GetLength(matrix, dimension);
T[] values = new T[s];
Min(matrix, dimension, result, values);
return result;
}
#endregion
#region Matrix Min/Max
/// <summary>
/// Gets the maximum value of a matrix.
/// </summary>
///
public static T Max<T>(this T[,] matrix)
where T : IComparable<T>
{
Tuple<int, int> index;
return Max(matrix, out index);
}
/// <summary>
/// Gets the minimum value of a matrix.
/// </summary>
///
public static T Min<T>(this T[,] matrix)
where T : IComparable<T>
{
Tuple<int, int> index;
return Min(matrix, out index);
}
/// <summary>
/// Gets the minimum values across one dimension of a matrix.
/// </summary>
///
public static T[] Min<T>(this T[,] matrix, int dimension)
where T : IComparable<T>
{
int s = GetLength(matrix, dimension);
var result = new T[s];
var indices = new int[s];
return Min(matrix, dimension, indices, result);
}
/// <summary>
/// Gets the maximum values across one dimension of a matrix.
/// </summary>
///
public static T[] Max<T>(this T[,] matrix, int dimension)
where T : IComparable<T>
{
int s = GetLength(matrix, dimension);
var result = new T[s];
var indices = new int[s];
return Max(matrix, dimension, indices, result);
}
/// <summary>
/// Gets the maximum values across one dimension of a matrix.
/// </summary>
///
public static T[] Max<T>(this T[,] matrix, int dimension, T[] result)
where T : IComparable<T>
{
int s = GetLength(matrix, dimension);
var indices = new int[s];
return Max(matrix, dimension, indices, result);
}
/// <summary>
/// Gets the minimum values across one dimension of a matrix.
/// </summary>
///
public static T[] Min<T>(this T[,] matrix, int dimension, T[] result)
where T : IComparable<T>
{
int s = GetLength(matrix, dimension);
var indices = new int[s];
return Min(matrix, dimension, indices, result);
}
/// <summary>
/// Gets the minimum values across one dimension of a matrix.
/// </summary>
///
public static T[] Min<T>(this T[,] matrix, int dimension, out int[] indices)
where T : IComparable<T>
{
int s = GetLength(matrix, dimension);
var result = new T[s];
indices = new int[s];
return Min(matrix, dimension, indices, result);
}
/// <summary>
/// Gets the maximum values across one dimension of a matrix.
/// </summary>
///
public static T[] Max<T>(this T[,] matrix, int dimension, out int[] indices)
where T : IComparable<T>
{
int s = GetLength(matrix, dimension);
var result = new T[s];
indices = new int[s];
return Max(matrix, dimension, indices, result);
}
/// <summary>
/// Gets the maximum values across one dimension of a matrix.
/// </summary>
///
public static T[] Max<T>(this T[,] matrix, int dimension, out int[] indices, T[] result)
where T : IComparable<T>
{
int s = GetLength(matrix, dimension);
indices = new int[s];
return Max(matrix, dimension, indices, result);
}
/// <summary>
/// Gets the minimum values across one dimension of a matrix.
/// </summary>
///
public static T[] Min<T>(this T[,] matrix, int dimension, out int[] indices, T[] result)
where T : IComparable<T>
{
int s = GetLength(matrix, dimension);
indices = new int[s];
return Min(matrix, dimension, indices, result);
}
#endregion
#region Core implementations
/// <summary>
/// Gets the maximum value of a matrix.
/// </summary>
///
public static T Max<T>(this T[,] matrix, out Tuple<int, int> imax)
where T : IComparable<T>
{
T max = matrix[0, 0];
imax = Tuple.Create(0, 0);
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
if (matrix[i, j].CompareTo(max) > 0)
{
max = matrix[i, j];
imax = Tuple.Create(i, j);
}
}
}
return max;
}
/// <summary>
/// Gets the minimum value of a matrix.
/// </summary>
///
public static T Min<T>(this T[,] matrix, out Tuple<int, int> imin)
where T : IComparable<T>
{
T min = matrix[0, 0];
imin = Tuple.Create(0, 0);
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
if (matrix[i, j].CompareTo(min) < 0)
{
min = matrix[i, j];
imin = Tuple.Create(i, j);
}
}
}
return min;
}
/// <summary>
/// Gets the maximum values across one dimension of a matrix.
/// </summary>
///
public static T[] Max<T>(this T[,] matrix, int dimension, int[] indices, T[] result)
where T : IComparable<T>
{
if (dimension == 1) // Search down columns
{
matrix.GetColumn(0, result: result);
for (int j = 0; j < matrix.GetLength(0); j++)
{
for (int i = 0; i < matrix.GetLength(1); i++)
{
if (matrix[j, i].CompareTo(result[j]) > 0)
{
result[j] = matrix[j, i];
indices[j] = i;
}
}
}
}
else
{
matrix.GetRow(0, result: result);
for (int j = 0; j < matrix.GetLength(1); j++)
{
for (int i = 0; i < matrix.GetLength(0); i++)
{
if (matrix[i, j].CompareTo(result[j]) > 0)
{
result[j] = matrix[i, j];
indices[j] = i;
}
}
}
}
return result;
}
/// <summary>
/// Gets the minimum values across one dimension of a matrix.
/// </summary>
///
public static T[] Min<T>(this T[,] matrix, int dimension, int[] indices, T[] result)
where T : IComparable<T>
{
if (dimension == 1) // Search down columns
{
matrix.GetColumn(0, result: result);
for (int j = 0; j < matrix.GetLength(0); j++)
{
for (int i = 0; i < matrix.GetLength(1); i++)
{
if (matrix[j, i].CompareTo(result[j]) < 0)
{
result[j] = matrix[j, i];
indices[j] = i;
}
}
}
}
else
{
matrix.GetRow(0, result: result);
for (int j = 0; j < matrix.GetLength(1); j++)
{
for (int i = 0; i < matrix.GetLength(0); i++)
{
if (matrix[i, j].CompareTo(result[j]) < 0)
{
result[j] = matrix[i, j];
indices[j] = i;
}
}
}
}
return result;
}
#endregion
#region GetRange
/// <summary>
/// Gets the maximum and minimum values in a vector.
/// </summary>
///
/// <param name="values">The vector whose min and max should be computed.</param>
/// <param name="min">The minimum value in the vector.</param>
/// <param name="max">The maximum value in the vector.</param>
///
/// <exception cref="System.ArgumentException">Raised if the array is empty.</exception>
///
public static void GetRange<T>(this T[] values, out T min, out T max)
where T : IComparable<T>
{
if (values.Length == 0)
{
min = max = default(T);
return;
}
min = max = values[0];
for (int i = 1; i < values.Length; i++)
{
if (values[i].CompareTo(min) < 0)
min = values[i];
if (values[i].CompareTo(max) > 0)
max = values[i];
}
}
/// <summary>
/// Gets the maximum and minimum values in a matrix.
/// </summary>
///
/// <param name="values">The vector whose min and max should be computed.</param>
/// <param name="min">The minimum value in the vector.</param>
/// <param name="max">The maximum value in the vector.</param>
///
/// <exception cref="System.ArgumentException">Raised if the array is empty.</exception>
///
public static void GetRange<T>(this T[,] values, out T min, out T max)
where T : IComparable<T>
{
if (values.Length == 0)
{
min = max = default(T);
return;
}
min = max = values[0, 0];
foreach (var v in values)
{
if (v.CompareTo(min) < 0)
min = v;
if (v.CompareTo(max) > 0)
max = v;
}
}
/// <summary>
/// Gets the maximum and minimum values in a vector.
/// </summary>
///
/// <param name="values">The vector whose min and max should be computed.</param>
///
/// <exception cref="System.ArgumentException">Raised if the array is empty.</exception>
///
public static IntRange GetRange(this int[] values)
{
int min, max;
GetRange(values, out min, out max);
return new IntRange(min, max);
}
/// <summary>
/// Gets the maximum and minimum values in a vector.
/// </summary>
///
/// <param name="values">The vector whose min and max should be computed.</param>
///
/// <exception cref="System.ArgumentException">Raised if the array is empty.</exception>
///
public static DoubleRange GetRange(this double[] values)
{
double min, max;
GetRange(values, out min, out max);
return new DoubleRange(min, max);
}
/// <summary>
/// Gets the maximum and minimum values in a vector.
/// </summary>
///
/// <param name="values">The vector whose min and max should be computed.</param>
///
/// <exception cref="System.ArgumentException">Raised if the array is empty.</exception>
///
public static IntRange GetRange(this int[,] values)
{
int min, max;
GetRange(values, out min, out max);
return new IntRange(min, max);
}
/// <summary>
/// Gets the maximum and minimum values in a vector.
/// </summary>
///
/// <param name="values">The vector whose min and max should be computed.</param>
///
/// <exception cref="System.ArgumentException">Raised if the array is empty.</exception>
///
public static DoubleRange GetRange(this double[,] values)
{
double min, max;
GetRange(values, out min, out max);
return new DoubleRange(min, max);
}
/// <summary>
/// Gets the range of the values across the columns of a matrix.
/// </summary>
///
/// <param name="value">The matrix whose ranges should be computed.</param>
/// <param name="dimension">
/// Pass 0 if the range should be computed for each of the columns. Pass 1
/// if the range should be computed for each row. Default is 0.
/// </param>
///
public static DoubleRange[] GetRange(this double[,] value, int dimension)
{
int rows = value.GetLength(0);
int cols = value.GetLength(1);
DoubleRange[] ranges;
if (dimension == 0)
{
ranges = new DoubleRange[cols];
for (int j = 0; j < ranges.Length; j++)
{
double max = value[0, j];
double min = value[0, j];
for (int i = 0; i < rows; i++)
{
if (value[i, j] > max)
max = value[i, j];
if (value[i, j] < min)
min = value[i, j];
}
ranges[j] = new DoubleRange(min, max);
}
}
else
{
ranges = new DoubleRange[rows];
for (int j = 0; j < ranges.Length; j++)
{
double max = value[j, 0];
double min = value[j, 0];
for (int i = 0; i < cols; i++)
{
if (value[j, i] > max)
max = value[j, i];
if (value[j, i] < min)
min = value[j, i];
}
ranges[j] = new DoubleRange(min, max);
}
}
return ranges;
}
/// <summary>
/// Deprecated. Please use GetRange() instead.
/// </summary>
///
[Obsolete("Please use GetRange instead.")]
public static IntRange Range(this int[] values)
{
return GetRange(values);
}
/// <summary>
/// Deprecated. Please use GetRange() instead.
/// </summary>
///
[Obsolete("Please use GetRange instead.")]
public static DoubleRange Range(this double[] values)
{
return GetRange(values);
}
/// <summary>
/// Deprecated. Please use GetRange() instead.
/// </summary>
///
[Obsolete("Please use GetRange instead.")]
public static IntRange Range(this int[,] values)
{
return GetRange(values);
}
/// <summary>
/// Deprecated. Please use GetRange() instead.
/// </summary>
///
[Obsolete("Please use GetRange instead.")]
public static DoubleRange Range(this double[,] values)
{
return GetRange(values);
}
/// <summary>
/// Deprecated. Please use GetRange() instead.
/// </summary>
///
[Obsolete("Please use GetRange instead.")]
public static DoubleRange[] Range(this double[,] value, int dimension)
{
return GetRange(value, dimension);
}
#endregion
}
}