sgorsten / linalg

linalg.h is a single header, public domain, short vector math library for C++
The Unlicense
854 stars 68 forks source link

`mul()` gives wrong answer? #23

Closed tjyuyao closed 4 years ago

tjyuyao commented 4 years ago
#include "linalg.h"
#include <iostream>

using namespace linalg::aliases;
using namespace linalg::ostream_overloads;
using namespace std;

int main() {

    float3x3 A = {
        {1.f, 0.f, 3.f},
        {0.f, 1.f, 3.f},
        {0.f, 0.f, 1.f},
    };

    float3x3 B = {
        {2.f, 0.f, 0.f},
        {0.f, 2.f, 0.f},
        {0.f, 0.f, 1.f},
    };

    cout << mul(A, B) << endl;

    /* 
     * the result output by this program is {{2,0,6},{0,2,6},{0,0,1}}, while by Octave:
     *
     * >> a = [ [1 0 3]; [0 1 3]; [0 0 1] ]
     * a =
     * 
     *    1   0   3
     *    0   1   3
     *    0   0   1
     * 
     * >> b = [[2 0 0]; [0 2 0]; [0 0 1]]
     * b =
     * 
     *    2   0   0
     *    0   2   0
     *    0   0   1
     * 
     * >> a * b
     * ans =
     * 
     *    2   0   3
     *    0   2   3
     *    0   0   1
     * */

    return 0;
}

while the document states mul(mat<T,M,N> a, mat<T,N,P> b) -> mat<T,M,P>, the library seems to calculate mul(b, a).

Is something I made wrong? or there is a bug in the library?

tjyuyao commented 4 years ago

Ohh, I understand why. I mixed the rows and columns of the initialization list.