| 1 | /************************************************************************* |
| 2 | * * |
| 3 | * Vega FEM Simulation Library Version 3.1 * |
| 4 | * * |
| 5 | * "matrix" library , Copyright (C) 2007 CMU, 2009 MIT * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Code author: Jernej Barbic * |
| 9 | * http://www.jernejbarbic.com/code * |
| 10 | * Research: Jernej Barbic, Doug L. James, Jovan Popovic * |
| 11 | * Funding: NSF, Link Foundation, Singapore-MIT GAMBIT Game Lab * |
| 12 | * * |
| 13 | * This library is free software; you can redistribute it and/or * |
| 14 | * modify it under the terms of the BSD-style license that is * |
| 15 | * included with this library in the file LICENSE.txt * |
| 16 | * * |
| 17 | * This library is distributed in the hope that it will be useful, * |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file * |
| 20 | * LICENSE.TXT for more details. * |
| 21 | * * |
| 22 | *************************************************************************/ |
| 23 | |
| 24 | #include "matrixMacros.h" |
| 25 | #include "matrixBLAS.h" |
| 26 | #include <math.h> |
| 27 | |
| 28 | template<class real> |
| 29 | real * SumMatrices(int m, int n, real * mtx1, real * mtx2, real * output) |
| 30 | { |
| 31 | real * target = output; |
| 32 | if (target == NULL) |
| 33 | target = (real*) malloc (sizeof(real) * m * n); |
| 34 | |
| 35 | int mn = m * n; |
| 36 | for(int i=0; i<mn; i++) |
| 37 | target[i] = mtx1[i] + mtx2[i]; |
| 38 | |
| 39 | return target; |
| 40 | } |
| 41 | |
| 42 | template<class real> |
| 43 | real * SubtractMatrices(int m, int n, real * mtx1, real * mtx2, real * output) |
| 44 | { |
| 45 | real * target = output; |
| 46 | if (target == NULL) |
| 47 | target = (real*) malloc (sizeof(real) * m * n); |
| 48 | |
| 49 | int mn = m * n; |
| 50 | for(int i=0; i<mn; i++) |
| 51 | target[i] = mtx1[i] - mtx2[i]; |
| 52 | |
| 53 | return target; |
| 54 | } |
| 55 | |
| 56 | template<class real> |
| 57 | real * MultiplyMatrices(int m, int p, int n, real * mtx1, real * mtx2, real * output) |
| 58 | { |
| 59 | real * target = output; |
| 60 | if (target == NULL) |
| 61 | target = (real*) malloc (sizeof(real) * m * n); |
| 62 | |
| 63 | for(int i=0; i<m; i++) |
| 64 | for(int j=0; j<n; j++) |
| 65 | { |
| 66 | real entry = 0; |
| 67 | for(int k=0; k<p; k++) |
| 68 | entry += mtx1[ELT(m,i,k)] * mtx2[ELT(p, k, j)]; |
| 69 | target[ELT(m, i, j)] = entry; |
| 70 | } |
| 71 | |
| 72 | return target; |
| 73 | } |
| 74 | |
| 75 | template<class real> |
| 76 | real * MultiplyMatricesT(int m, int p, int n, real * mtx1, real * mtx2, real * output) |
| 77 | { |
| 78 | real * target = output; |
| 79 | if (target == NULL) |
| 80 | target = (real*) malloc (sizeof(real) * m * n); |
| 81 | |
| 82 | for(int i=0; i<m; i++) |
| 83 | for(int j=0; j<n; j++) |
| 84 | { |
| 85 | real entry = 0; |
| 86 | for(int k=0; k<p; k++) |
| 87 | entry += mtx1[ELT(p, k, i)] * mtx2[ELT(p, k, j)]; |
| 88 | target[ELT(m, i, j)] = entry; |
| 89 | } |
| 90 | |
| 91 | return target; |
| 92 | } |
| 93 | |
| 94 | template<class real> |
| 95 | real * ScalarMultiplyMatrix(int m, int n, real alpha, real * mtx, real * output) |
| 96 | { |
| 97 | real * target = output; |
| 98 | if (target == NULL) |
| 99 | target = (real*) malloc (sizeof(real) * m * n); |
| 100 | |
| 101 | int mn = m * n; |
| 102 | for(int i=0; i<mn; i++) |
| 103 | target[i] = alpha * mtx[i]; |
| 104 | |
| 105 | return target; |
| 106 | } |
| 107 | |
| 108 | // computes Euclidean norm of a vector |
| 109 | template <class real> |
| 110 | real VectorNorm(int m, real * vec) |
| 111 | { |
| 112 | double norm = 0; |
| 113 | for(int i=0; i<m; i++) |
| 114 | norm += vec[i] * vec[i]; |
| 115 | return (sqrt(norm)); |
| 116 | } |
| 117 |