This is the implementation. Is there any way to avoid the casting?
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.");
}