Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added comparison operations

  • Loading branch information...
commit b63b01afdd1128be5f94dd33862426d54ae6a747 1 parent 00c9826
@sporgj authored
Showing with 75 additions and 10 deletions.
  1. +52 −5 src/objects/numeric.cpp
  2. +23 −5 src/objects/numeric.h
View
57 src/objects/numeric.cpp
@@ -9,8 +9,8 @@
#include "numeric.h"
namespace _Y_OBJECT_{
-Numeric* Numeric::fromObject(Object* o){
- return dynamic_cast<Numeric*>(o);
+Numeric* Numeric::fromObject(Object* o, bool make_new){
+ return make_new ? new Numeric(*dynamic_cast<Numeric*>(o)) : dynamic_cast<Numeric*>(o);
}
Numeric::Numeric(size_t x, size_t y) {
@@ -40,8 +40,7 @@ Numeric& Numeric::operator+=(const Numeric& n2){
}
return *this;
}
- //FIXME throw an exception
- throw ("the matrices do not have the same dimension");
+ error_reporting ("the matrices do not have the same dimension");
}
inline Numeric Numeric::operator-(const Numeric& n2){
@@ -58,7 +57,7 @@ Numeric& Numeric::operator-=(const Numeric& n2){
return *this;
}
//FIXME throw an exception
- throw ("the matrices do not have the same dimension");
+ error_reporting ("the matrices do not have the same dimension");
}
inline Numeric Numeric::operator*(const Numeric& n2){
@@ -121,6 +120,54 @@ Numeric& Numeric::exponent(const Numeric& n2){
return *this;
}
+bool Numeric::operator <(const Numeric& n2){
+ //check that they have one element each
+ if(data->size()!=1 && n2.data->size()!=1)
+ error_reporting ("cannot compare two matrices");
+
+ //else, we just return
+ Number& d = data->operator [](0);
+ Number& d1 = n2.data->operator [](0);
+
+ return d.real()<d1.real();
+}
+
+bool Numeric::operator >(const Numeric& n2){
+ //check that they have one element each
+ if(data->size()!=1 && n2.data->size()!=1)
+ error_reporting ("cannot compare two matrices");
+
+ //else, we just return
+ Number& d = data->operator [](0);
+ Number& d1 = n2.data->operator [](0);
+
+ return d.real()>d1.real();
+}
+
+bool Numeric::operator <=(const Numeric& n2){
+ //check that they have one element each
+ if(data->size()!=1 && n2.data->size()!=1)
+ error_reporting ("cannot compare two matrices");
+
+ //else, we just return
+ Number& d = data->operator [](0);
+ Number& d1 = n2.data->operator [](0);
+
+ return d.real()<=d1.real();
+}
+
+bool Numeric::operator >=(const Numeric& n2){
+ //check that they have one element each
+ if(data->size()!=1 && n2.data->size()!=1)
+ error_reporting ("cannot compare two matrices");
+
+ //else, we just return
+ Number& d = data->operator [](0);
+ Number& d1 = n2.data->operator [](0);
+
+ return d.real()>=d1.real();
+}
+
Numeric::~Numeric() {
delete data;
}
View
28 src/objects/numeric.h
@@ -9,19 +9,23 @@
#ifndef NUMERIC_H_
#define NUMERIC_H_
-#include "../backbone/core/types.h"
-#include "object.h"
#include <cmath>
#include <complex>
#include <vector>
#include <ostream>
+#include "../backbone/core/types.h"
+#include "object.h"
+#include "../backbone/global.h"
+
namespace _Y_OBJECT_{
using std::vector;
using std::complex;
using std::ostream;
+using _Y_BACKBONE_::error_reporting;
+
typedef complex<ydouble> Number;
class Numeric : public Object {
@@ -42,9 +46,10 @@ class Numeric : public Object {
* This does the casting operation. For the different Object types, different Numeric objects
* are returned
* @param o is the operation to cast.
+ * @param make_new if to create a new object from the object. Defaults to false.
* @return the corresponding Numeric object, 0 (null) if it could not be casted.
*/
- static Numeric* fromObject(Object* o);
+ static Numeric* fromObject(Object* o, bool make_new=false);
size_t rows() const{return r;}
size_t cols() const{return c;}
@@ -62,13 +67,13 @@ class Numeric : public Object {
return data->operator[](r*x+y);
}
//throw an exception
- throw ("Row/Column index is out of range");
+ error_reporting("Row/Column index is out of range");
}
Number& operator[] (size_t index){
if(index<data->size())
return data->operator [](index);
- throw ("index out of range");
+ error_reporting("index out of range");
}
/**
@@ -80,6 +85,19 @@ class Numeric : public Object {
}
}
+ vector<Number>::iterator first(){
+ return data->begin();
+ }
+
+ vector<Number>::iterator last(){
+ return data->end();
+ }
+
+ bool operator< (const Numeric& n2);
+ bool operator> (const Numeric& n2);
+ bool operator>= (const Numeric& n2);
+ bool operator<= (const Numeric& n2);
+
Numeric operator+(const Numeric& n2);
Numeric& operator+=(const Numeric& n2);
Numeric operator-(const Numeric& n2);
Please sign in to comment.
Something went wrong with that request. Please try again.