News Uni Dev Res Blag

SEPPLIX

2010/11/05 - 04:13

/* horrible matrix templates, don't use
 * Copyright (C) 2010  ed <irc.rizon.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, please refer to the following URL:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */
   I still think all this whitespace is retarded

makefile

just do it yourself you lazy cunt

templatrix.h

template <class T>
T **matrix_create(int sy, int sx, T *v)
{
    T **m = new T *[sy];
    for (int y = 0; y < sy; y++)
    {
        m[y] = new T[sx];
        for (int x = 0; x < sx; x++)
        {
            m[y][x] = v[(sx*y)+x];
        }
    }
    return m;
}

template <class T>
T **matrix_add(T **ma, int ay, int ax, T **mb, int by, int bx)
{
    if (ax != bx || ay != by)
        throw 9001;
        
    T **mc = new T *[ay];
    for (int y = 0; y < ay; y++)
    {
        mc[y] = new T[ax];
        for (int x = 0; x < ax; x++)
        {
            mc[y][x] = ma[y][x] + mb[y][x];
        }
    }
    return mc;
}

template <class T>
T **matrix_sub(T **ma, int ay, int ax, T **mb, int by, int bx)
{
    if (ax != bx || ay != by)
        throw 9001;

    T **mc = new T *[ay];
    for (int y = 0; y < ay; y++)
    {
        mc[y] = new T[ax];
        for (int x = 0; x < ax; x++)
        {
            mc[y][x] = ma[y][x] - mb[y][x];
        }
    }
    return mc;
}

template <class T>
T **matrix_mul(T **ma, int ay, int ax, T **mb, int by, int bx)
{
    // Knowledge borrowed from wikipædia:
    // Multiplying matrices AxB and CxD results in AxD
    // Furthermore, B must equal C
    
    //          [ b b b ]
    //          [ b b b ]
    //              | |
    // [ a a ]--[---X | ]
    // [ a a ]  [ • • | ]
    // [ a a ]--[-----X ]
    // [ a a ]  [ • • • ]
    
    if (ax != by)
        throw 9001;
    
    T **mc = new T *[ay];
    for (int y = 0; y < ay; y++)
    {
        mc[y] = new T[bx];
        for (int x = 0; x < bx; x++)
        {
            int v = 0;
            for (int z = 0; z < ax; z++)
            {
                v += ma[y][z] * mb[z][x];
            }
            mc[y][x] = v;
        }
    }
    return mc;
}

main.cpp

#include <cstdio>
#include <iostream>
#include "matrixer.h"
using namespace std;

// SOURCES USED:
// http://en.wikipedia.org/wiki/Matrix_multiplication

void print(int **mat, int y, int x)
{
    for (int b = 0; b < y; b++)
    {
        for (int a = 0; a < x; a++)
        {
            printf("%4d", mat[b][a]);
        }
        cout << endl;
    }
    cout << endl;
}

int main(int argc, char **argv)
{
    int mar[] = {
        0, -1, 2,
        4, 11, 2
    };
    int mbr[] = {
        1, 2, 3,
        4, 5, 6
    };
    int mcr[] = {
        3, -1,
        1,  2,
        6,  1
    };
    
    // Convert the `raw' 1-dimensional matrices into 2 dimensional ones
    int **ma = matrix_create(2, 3, mar);
    int **mb = matrix_create(2, 3, mbr);
    int **mc = matrix_create(3, 2, mcr);
    
    cout << "Matrix A - base matrix for operations\n";
    print(ma, 2, 3);
    
    cout << "Matrix B - addition and subtraction\n";
    print(mb, 2, 3);
    
    cout << "Matric C - multiplication\n";
    print(mc, 3, 2);
    
    // Performing the matrix operations
    int **add = matrix_add(ma, 2, 3, mb, 2, 3);
    int **sub = matrix_sub(ma, 2, 3, mb, 2, 3);
    int **mul = matrix_mul(ma, 2, 3, mc, 3, 2);
    
    cout << "Addition of A and B:\n";
    print(add, 2, 3);
    
    cout << "Subtracting B from A:\n";
    print(sub, 2, 3);
    
    cout << "Multiplication of A and B:\n";
    print(mul, 2, 2);
}


Copyright © 2010 Ed Edland