Generic Function that multiplies Matricies


I want to write something that takes 2 Vectors and squares them and returns the value of the 1x1 Matrix. i.e value = (v_t*v).index(0);

My attempt

fn sqaure <R1 : Dim + DimName,R2 : Dim + DimName,C1 : Dim + DimName,C2 :Dim + DimName> (m1 : MatrixMN<MyType,R1,C1>, m2 : MatrixMN<MyType,R2,C2> -> MyType where ShapeConstraint: AreMultipliable<R1, C1, R2, C2> { let temp = m1*m2; *return temp.index(0); }

But I cant get it to work. I gave up after no implementation for <R2 as image_proc::image::na::DimName>::Value * <C2 as image_proc::image::na::DimName>::Value error.
Is there an easy way to do this, that I am missing?


Here is something that compiles:

use std::ops::{Add, AddAssign, Mul, MulAssign};
use na::{
    RealField, MatrixMN, Dim, DefaultAllocator,
    constraint::{ShapeConstraint, AreMultipliable}

fn square<MyType, R1, R2, C1, C2>(
    m1 : MatrixMN<MyType, R1, C1>,
    m2 : MatrixMN<MyType, R2, C2>)
    -> MyType
    where MyType: RealField,
          R1: Dim, R2: Dim, C1: Dim, C2: Dim,
          ShapeConstraint: AreMultipliable<R1, C1, R2, C2>,
          DefaultAllocator: Allocator<MyType, R1, C1> +
                            Allocator<MyType, R2, C2> +
                            Allocator<MyType, R1, C2> {
    let temp = m1 * m2;
    return temp[0];

I’ve added the MyType type parameter for it to compile but I guess it was already defined elsewhere in your code. What you were missing was the DefaultAllocator: Allocator bounds. This is responsible for knowing how to represent the internal storage of the various MatrixMN, as well as the result of the multiplication m1 * m2. When an Allocator bound is missing, you will get this kind of errors related to DimName, which is quite misleading since the DimName bound isn’t necessary at all unless you only want to allow statically-sized matrices (this will disallow DMatrix). If you only use the Dim bound, then the input matrix can be both dynamically sized or statically sized.