Jamoma API  0.6.0.a19
TTMatrixMath.cpp
1 /*
2  * Support for mathematical operations on the
3  * Jamoma N-Dimensional Matrix Data Class
4  * Copyright © 2011, Timothy Place
5  *
6  * License: This code is licensed under the terms of the "New BSD License"
7  * http://creativecommons.org/licenses/BSD/
8  */
9 
10 #include "TTFoundationAPI.h"
11 #include "TTEnvironment.h"
12 
13 
14 // Macro so that we don't have to duplicate this cumbersome and ugly if statement many many times
15 // TYPECHANGE
16 #define TTMATRIX_CALL_ITERATOR_3ARGS_WITH_TEMPLATE(type,a,b,c,template) { \
17  if (type == kTypeUInt8) \
18  TTMatrixBase::iterateWhenAllAttributesMatch(a, b, c, template<TTUInt8>); \
19  else if (type == kTypeInt32) \
20  TTMatrixBase::iterateWhenAllAttributesMatch(a, b, c, template<TTInt32>); \
21  else if (type == kTypeFloat32) \
22  TTMatrixBase::iterateWhenAllAttributesMatch(a, b, c, template<TTFloat32>); \
23  else if (type == kTypeFloat64) \
24  TTMatrixBase::iterateWhenAllAttributesMatch(a, b, c, template<TTFloat64>); }
25 
26 
27 
28 template<typename T>
29 void TTMatrixBaseIteratorAdd(TTPtr c, const TTPtr a, const TTPtr b)
30 {
31  T* cc = (T*)c;
32  T aa = *(T*)a;
33  T bb = *(T*)b;
34 
35  *cc = aa + bb;
36 }
37 
38 
39 template<typename T>
40 void TTMatrixBaseIteratorSubtract(TTPtr c, const TTPtr a, const TTPtr b)
41 {
42  T* cc = (T*)c;
43  T aa = *(T*)a;
44  T bb = *(T*)b;
45 
46  *cc = aa - bb;
47 }
48 
49 
51 {
52  const TTMatrixBase* A = this;
53  const TTMatrixBase* B = &other;
54  TTMatrixBase* C = NULL;
55 
56  try {
57  C = new TTMatrixBase(kTTSymEmpty);
58  TTMATRIX_CALL_ITERATOR_3ARGS_WITH_TEMPLATE(A->mType, C, A, B, TTMatrixBaseIteratorAdd); //TYPECHANGE
59  } catch (...) {
60  // return kTTErrInstantiateFailed;
61  // NW: since method does not return a type TTErr, we will simply catch this expection and return a NULL TTMatrixBasePtr
62  }
63 
64  return C;
65 }
66 
67 
69 {
70  const TTMatrixBase* A = this;
71  const TTMatrixBase* B = &other;
72  TTMatrixBase* C = NULL;
73 
74  try {
75  C = new TTMatrixBase(kTTSymEmpty);
76  TTMATRIX_CALL_ITERATOR_3ARGS_WITH_TEMPLATE(A->mType, C, A, B, TTMatrixBaseIteratorSubtract); //TYPECHANGE
77  } catch (...) {
78  // return kTTErrInstantiateFailed;
79  // NW: since method does not return a type TTErr, we will simply catch this expection and return a NULL TTMatrixBasePtr
80  }
81 
82  return C;
83 }
84 
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
2-dimensional matrix of compound values with N elements each.
Definition: TTMatrixBase.h:41
TTEnvironment is a global object providing information on the environemt.
TTMatrixBase * operator-(const TTMatrixBase &B) const
Subtract matrix B from matrix A to produce matrix C.
TTMatrixBase * operator+(const TTMatrixBase &B) const
Add two matrices to produce a third matrix.
TTDataType mType
member of global enumerated list #TTBase::TTDataType (i.e., kTypeUInt8, kTypeUInt16, kTypeInt32, kTypeUInt64, kTypeFloat32, kTypeFloat64)
Definition: TTMatrixBase.h:53