summaryrefslogtreecommitdiff
path: root/src/value.cc
diff options
context:
space:
mode:
authorAndrew Plumb <andrew@plumb.org>2012-02-15 02:22:48 (GMT)
committerAndrew Plumb <andrew@plumb.org>2012-02-15 02:22:48 (GMT)
commit5d6a259a8b64810e3e6bae6c3da9e018e9d8927f (patch)
tree0b88417db5bef0b7afb9683bde228e263c9f7eac /src/value.cc
parent44ff85399011ea1bd3eb812ce97d312ccc4f32eb (diff)
Added vector math support.
Added vector dot-product, matrix*vec, vec*matrix and matrix*matrix support.
Diffstat (limited to 'src/value.cc')
-rw-r--r--src/value.cc59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/value.cc b/src/value.cc
index 47fac1e..48fea1a 100644
--- a/src/value.cc
+++ b/src/value.cc
@@ -30,6 +30,7 @@
#include <sstream>
#include <QDir>
#include <boost/foreach.hpp>
+#include "printutils.h"
Value::Value()
{
@@ -157,6 +158,64 @@ Value Value::operator * (const Value &v) const
if (this->type == NUMBER && v.type == NUMBER) {
return Value(this->num * v.num);
}
+ if (this->type == VECTOR && v.type == VECTOR && this->vec.size() == v.vec.size() ) {
+ if ( this->vec[0]->type == NUMBER && v.vec[0]->type == NUMBER ) {
+ // Vector dot product.
+ double r=0.0;
+ for (size_t i=0; i <this->vec.size(); i++) {
+ if ( this->vec[i]->type != NUMBER || v.vec[i]->type != NUMBER ) return Value();
+ r = r + (this->vec[i]->num * v.vec[i]->num);
+ }
+ return Value(r);
+ } else if ( this->vec[0]->type == VECTOR && v.vec[0]->type == NUMBER ) {
+ // Matrix * Vector
+ Value r;
+ r.type = VECTOR;
+ for ( size_t i=0; i < this->vec.size(); i++) {
+ double r_e=0.0;
+ if ( this->vec[i]->vec.size() != v.vec.size() ) return Value();
+ for ( size_t j=0; j < this->vec[i]->vec.size(); j++) {
+ if ( this->vec[i]->vec[j]->type != NUMBER || v.vec[i]->type != NUMBER ) return Value();
+ r_e = r_e + (this->vec[i]->vec[j]->num * v.vec[j]->num);
+ }
+ r.vec.push_back(new Value(r_e));
+ }
+ return r;
+ } else if (this->vec[0]->type == NUMBER && v.vec[0]->type == VECTOR ) {
+ // Vector * Matrix
+ Value r;
+ r.type = VECTOR;
+ for ( size_t i=0; i < v.vec[0]->vec.size(); i++) {
+ double r_e=0.0;
+ for ( size_t j=0; j < v.vec.size(); j++) {
+ if ( v.vec[j]->vec.size() != v.vec[0]->vec.size() ) return Value();
+ if ( this->vec[j]->type != NUMBER || v.vec[j]->vec[i]->type != NUMBER ) return Value();
+ r_e = r_e + (this->vec[j]->num * v.vec[j]->vec[i]->num);
+ }
+ r.vec.push_back(new Value(r_e));
+ }
+ return r;
+ }
+ }
+ if (this->type == VECTOR && v.type == VECTOR && this->vec[0]->type == VECTOR && v.vec[0]->type == VECTOR && this->vec[0]->vec.size() == v.vec.size() ) {
+ // Matrix * Matrix
+ Value rrow;
+ rrow.type = VECTOR;
+ for ( size_t i=0; i < this->vec.size(); i++ ) {
+ Value * rcol=new Value();
+ rcol->type = VECTOR;
+ for ( size_t j=0; j < this->vec.size(); j++ ) {
+ double r_e=0.0;
+ for ( size_t k=0; k < v.vec.size(); k++ ) {
+ r_e = r_e + (this->vec[i]->vec[k]->num * v.vec[k]->vec[j]->num);
+ }
+ // PRINTB(" r_e = %s",r_e);
+ rcol->vec.push_back(new Value(r_e));
+ }
+ rrow.vec.push_back(rcol);
+ }
+ return rrow;
+ }
return Value();
}
contact: Jan Huwald // Impressum