| 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 <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <ctype.h> |
| 28 | |
| 29 | #define CBLAS |
| 30 | #ifdef CBLAS |
| 31 | #include "lapack-headers.h" |
| 32 | #endif |
| 33 | |
| 34 | #include "matrixIO.h" |
| 35 | #include "matrixMacros.h" |
| 36 | #include "matrixProjection.h" |
| 37 | |
| 38 | #ifdef CBLAS |
| 39 | // if-then-else template choosing |
| 40 | template<bool C> |
| 41 | class _cblas_xgemv {}; |
| 42 | |
| 43 | template<> |
| 44 | class _cblas_xgemv<true> { |
| 45 | public: |
| 46 | inline static void f (const CBLAS_ORDER order, |
| 47 | const CBLAS_TRANSPOSE trans, const int m, const int n, |
| 48 | const float alpha, const float * a, const int lda, |
| 49 | const float * x, const int incx, float beta, |
| 50 | float * y, const int incy) |
| 51 | { cblas_sgemv(order, trans, m, n, alpha, a, lda, x, incx, beta, y, incy);} |
| 52 | }; |
| 53 | |
| 54 | template<> |
| 55 | class _cblas_xgemv<false> { |
| 56 | public: |
| 57 | inline static void f (const CBLAS_ORDER order, |
| 58 | const CBLAS_TRANSPOSE trans, const int m, const int n, |
| 59 | const double alpha, const double * a, const int lda, |
| 60 | const double * x, const int incx, double beta, |
| 61 | double * y, const int incy) |
| 62 | { cblas_dgemv(order, trans, m, n, alpha, a, lda, x, incx, beta, y, incy);} |
| 63 | }; |
| 64 | #endif |
| 65 | |
| 66 | // construct vector f, using the equation f = U q |
| 67 | // matrix U is of size n x r, and is given in the column-major order |
| 68 | // vector q is of length r |
| 69 | // vector f is of length n |
| 70 | template <class real> |
| 71 | void SynthesizeVector(int n_inp, int r_inp, real * U_inp, real * q_inp, real * f_inp) |
| 72 | { |
| 73 | #ifdef CBLAS |
| 74 | CBLAS_ORDER order= CblasColMajor; |
| 75 | CBLAS_TRANSPOSE trans= CblasNoTrans; |
| 76 | int m = n_inp; |
| 77 | int n = r_inp; |
| 78 | real alpha = 1; |
| 79 | real * a = U_inp; |
| 80 | int lda = m; |
| 81 | real * x = q_inp; |
| 82 | int incx = 1; |
| 83 | real beta = 0; |
| 84 | real * y = f_inp; // holds the result |
| 85 | int incy = 1; |
| 86 | |
| 87 | _cblas_xgemv<sizeof(real)==sizeof(float)>::f(order, trans, m, n, alpha, a, lda, x, incx, beta, y, incy); |
| 88 | #else |
| 89 | int i,j; |
| 90 | for (i=0; i<n_inp; i++) // over all rows, i.e. all coordinates |
| 91 | { |
| 92 | f_inp[i] = 0; |
| 93 | for (j=0; j<r_inp; j++) // over all modes |
| 94 | f_inp[i] += U_inp[ELT(n_inp,i,j)] * q_inp[j]; |
| 95 | } |
| 96 | #endif |
| 97 | } |
| 98 | |
| 99 | // construct sparse vector f, using the equation f = U q |
| 100 | // matrix U is of size n x r, and is given in the column-major order |
| 101 | // vector q is of length r |
| 102 | // the coordinates of the vector f of interest are specified in the array i_buffer |
| 103 | // k is the total number of requested coordinates |
| 104 | // result is written into vector f. Length of result is k <= n |
| 105 | template <class real> |
| 106 | void SynthesizeSparseVector(int n, int r, real * U, real * q, real * f, int * i_buffer, int k) |
| 107 | { |
| 108 | int i,j; |
| 109 | for (i=0; i<k; i++) // over all non-zero rows |
| 110 | { |
| 111 | f[i] = 0; |
| 112 | for (j=0; j<r; j++) // over all modes |
| 113 | f[i] += U[ELT(n,i_buffer[i],j)] * q[j]; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | // project vector f onto basis, given by U, using the equation q = U^T f |
| 119 | // matrix U is of size n x r |
| 120 | template <class real> |
| 121 | void ProjectVector(int n_inp, int r_inp, real * U_inp, real * q_inp, real * f_inp) |
| 122 | { |
| 123 | // has to make inner product of vector f will all the columns of U |
| 124 | // i.e. multiply U^T * f = q |
| 125 | #ifdef CBLAS |
| 126 | CBLAS_ORDER order= CblasColMajor; |
| 127 | CBLAS_TRANSPOSE trans= CblasTrans; |
| 128 | int m = n_inp; |
| 129 | int n = r_inp; |
| 130 | real alpha = 1; |
| 131 | real * a = U_inp; |
| 132 | int lda = m; |
| 133 | real * x = f_inp; |
| 134 | int incx = 1; |
| 135 | real beta = 0; |
| 136 | real * y = q_inp; // will hold the result |
| 137 | int incy = 1; |
| 138 | |
| 139 | _cblas_xgemv<sizeof(real)==sizeof(float)>::f(order, trans, m, n, alpha, a, lda, x, incx, beta, y, incy); |
| 140 | #else |
| 141 | int i,j; |
| 142 | for (j=0; j<r_inp; j++) // over all columns of U |
| 143 | { |
| 144 | // dot product of column j of U with vector f |
| 145 | q_inp[j] = 0; |
| 146 | for (i=0; i<n_inp; i++) |
| 147 | q_inp[j] += U_inp[ELT(n_inp,i,j)] * f_inp[i]; |
| 148 | } |
| 149 | #endif |
| 150 | } |
| 151 | |
| 152 | // project sparse vector f onto basis, given by U, using the equation q = U^T f |
| 153 | // matrix U is of size n x r |
| 154 | // number of non-zero components of f is given by k |
| 155 | // non-zero components of vector f are given by f_buffer and i_buffer, where i_buffer[j] specifies the position, |
| 156 | // and f_buffer[j] specifies the force component at i_buffer[j] |
| 157 | template <class real> |
| 158 | void ProjectSparseVector(int n, int r, real * U, real * q, real * f_buffer, int * i_buffer, int k) |
| 159 | { |
| 160 | // has to make inner product of vector f will all the columns of U |
| 161 | int i,j; |
| 162 | for (j=0; j<r; j++) // over all columns of U |
| 163 | { |
| 164 | // dot product of column j of U with vector f |
| 165 | q[j] = 0; |
| 166 | for (i=0; i<k; i++) |
| 167 | q[j] += U[ELT(n,i_buffer[i],j)] * f_buffer[i]; |
| 168 | } |
| 169 | |
| 170 | } |
| 171 | |
| 172 | template void SynthesizeVector<double>(int n, int r, double * U, double * q, double * f); |
| 173 | template void SynthesizeVector<float>(int n, int r, float * U, float * q, float * f); |
| 174 | |
| 175 | template void SynthesizeSparseVector<double>(int n, int r, double * U, double * q, double * f, int * i_buffer, int k); |
| 176 | template void SynthesizeSparseVector<float>(int n, int r, float * U, float * q, float * f, int * i_buffer, int k); |
| 177 | |
| 178 | template void ProjectVector<double>(int n, int r, double * U, double * q, double * f); |
| 179 | template void ProjectVector<float>(int n, int r, float * U, float * q, float * f); |
| 180 | |
| 181 | template void ProjectSparseVector<double>(int n, int r, double * U, double * q, double * f_buffer, int * i_buffer, int k); |
| 182 | template void ProjectSparseVector<float>(int n, int r, float * U, float * q, float * f_buffer, int * i_buffer, int k); |
| 183 |