thank you for the help and the great explanation,
really fast and informative.
Thank you,
michael
really fast and informative.
Thank you,
michael
The type referenced through 'MathNet.Numerics.LinearAlgebra.Matrix`1' is defined in an assembly that is not referenced. You must add a reference to assembly 'MathNet.Numerics'.Any thoughts on what is going wrong? The dlls were acquired through nuget. They are version 3.0.2.
#r @"C:\path_to_dlls\MathNet.Numerics.dll"
#r @"C:\path_to_dlls\Mathnet.Numerics.FSharp.dll"
open MathNet.Numerics
open MathNet.Numerics.LinearAlgebra
open MathNet.Numerics.LinearAlgebra.Double
SpecialFunctions.Gamma(0.5)
let m : Matrix<float> = DenseMatrix.randomStandard 50 50
(m * m.Transpose()).Determinant()
Functions.Sum(v, Axis.Rows)
do and how does it relate to ones * diag(v)
, exactly?FoldRows(f,state)
which applies a binary function to each row vector of the matrix and a state vector (where the result becomes the state for the next row)? See also "Enumerators and Higher Order Functions" in Matrices and Vectors.FoldRows(f,state)
is what I need to build it. for (int majorItem = 0; majorItem < t.Length; majorItem++)
{
for (int i = 0; i < items; i++)
t[majorItem] += mt[ptr + i * minorStride];
ptr += majorStride;
}
It treats every column of the matrix M as a vector mv and adds the vector v to it.For every k in M
mv = M.GetColumnVector(k);
mv += v ;
m.MapIndexedInplace((i,j,x) => x + v.At(i));
public static Matrix<T> AddVectorOnEachColumn<T>(this Matrix<T> m1, Vector<T> v, T scale) where T : struct,
IEquatable<T>, IFormattable
{
Contract.Requires(m1 != null);
Contract.Requires(v != null);
if (m1 is Matrix<float>)
{
var fm1 = m1 as Matrix<float>;
var fv = v as Vector<float>;
var fscale = (float)(object)scale;
return fm1.MapIndexed((i, j, value) => fm1.At(i, j) + fscale * fv.At(j)) as Matrix<T>;
}
else if (m1 is Matrix<double>)
{
var dm1 = m1 as Matrix<double>;
var dv = v as Vector<double>;
var dscale = (double)(object)scale;
return dm1.MapIndexed((i, j, value) => dm1.At(i, j) + dscale * dv.At(j)) as Matrix<T>;
}
else throw new NotSupportedException("Type: {0} is not supported by the Matrix<T> class.");
}
dm1.At(i, j)
is already provided as value
:fm1.MapIndexed((i, j, value) => value + fscale * fv.At(j)) as Matrix<T>
Then you can pre-scale the vector, once, which is supported generically; getting rid of some of the casting:var fm1 = m1 as Matrix<float>;
var fsv = (v * scale) as Vector<float>;
return fm1.MapIndexed((i, j, value) => value + fsv.At(j)) as Matrix<T>;
Do you actually need the extension function to be generic? If not, you can just overload per type individually, without any casting:// Inplace:
public static void AddScaledVectorToEachColumnInplace(this Matrix<double> m, Vector<double> v, double scale)
{
double[] scaled = (v * scale).ToArray();
m.MapIndexedInplace((i,j,x) => x + scaled[i], Zeros.Include);
}
public static void AddScaledVectorToEachColumnInplace(this Matrix<float> m, Vector<float> v, float scale)
{
float[] scaled = (v * scale).ToArray();
m.MapIndexedInplace((i,j,x) => x + scaled[i], Zeros.Include);
}
// Outplace:
public static Matrix<double> AddScaledVectorToEachColumn(this Matrix<double> m, Vector<double> v, double scale)
{
double[] scaled = (v * scale).ToArray();
return m.MapIndexed((i,j,x) => x + scaled[i], Zeros.Include);
}