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);
}