From: Christian Bauer Date: Thu, 20 Feb 2003 03:33:18 +0000 (+0000) Subject: - added non-const operator[] X-Git-Tag: release_1-1-0~26 X-Git-Url: https://www.ginac.de/ginac.git/tutorial/ginac.git?a=commitdiff_plain;h=9c1472dc2450991710565e1eec18ee343128fe08;p=ginac.git - added non-const operator[] - removed let_op() for power and relational - removed redundant matrix::op() --- diff --git a/ginac/basic.cpp b/ginac/basic.cpp index 74ffe98d..9fd73982 100644 --- a/ginac/basic.cpp +++ b/ginac/basic.cpp @@ -185,7 +185,7 @@ ex basic::op(int i) const /** Return modifyable operand/member at position i. */ ex & basic::let_op(int i) { - throw(std::out_of_range("op() out of range")); + throw(std::runtime_error(std::string("let_op() not implemented by ") + class_name())); } ex basic::operator[](const ex & index) const @@ -201,6 +201,19 @@ ex basic::operator[](int i) const return op(i); } +ex & basic::operator[](const ex & index) +{ + if (is_exactly_a(index)) + return let_op(ex_to(index).to_int()); + + throw(std::invalid_argument("non-numeric indices not supported by this type")); +} + +ex & basic::operator[](int i) +{ + return let_op(i); +} + /** Test for occurrence of a pattern. An object 'has' a pattern if it matches * the pattern itself or one of the children 'has' it. As a consequence * (according to the definition of children) given e=x+y+z, e.has(x) is true diff --git a/ginac/basic.h b/ginac/basic.h index 320b247f..57cf5734 100644 --- a/ginac/basic.h +++ b/ginac/basic.h @@ -98,9 +98,11 @@ public: // only const functions please (may break reference counting) virtual bool info(unsigned inf) const; virtual unsigned nops() const; virtual ex op(int i) const; - virtual ex & let_op(int i); virtual ex operator[](const ex & index) const; virtual ex operator[](int i) const; + virtual ex & let_op(int i); + virtual ex & operator[](const ex & index); + virtual ex & operator[](int i); virtual ex expand(unsigned options = 0) const; virtual bool has(const ex & other) const; virtual ex map(map_function & f) const; diff --git a/ginac/clifford.cpp b/ginac/clifford.cpp index f1c710d5..0918fe42 100644 --- a/ginac/clifford.cpp +++ b/ginac/clifford.cpp @@ -705,7 +705,7 @@ ex canonicalize_clifford(const ex & e) // Expand product, if necessary ex rhs_expanded = rhs.expand(); if (!is_a(rhs_expanded)) { - srl.let_op(i) = (lhs == canonicalize_clifford(rhs_expanded)); + srl[i] = (lhs == canonicalize_clifford(rhs_expanded)); continue; } else if (!is_a(rhs.op(0))) @@ -732,7 +732,7 @@ ex canonicalize_clifford(const ex & e) it[0] = save1; it[1] = save0; sum -= ncmul(v, true); - srl.let_op(i) = (lhs == canonicalize_clifford(sum)); + srl[i] = (lhs == canonicalize_clifford(sum)); goto next_sym; } ++it; diff --git a/ginac/ex.cpp b/ginac/ex.cpp index 8ed63d80..d4c37a0a 100644 --- a/ginac/ex.cpp +++ b/ginac/ex.cpp @@ -130,24 +130,26 @@ bool ex::find(const ex & pattern, lst & found) const return any_found; } -ex ex::operator[](const ex & index) const +/** Return modifyable operand/member at position i. */ +ex & ex::let_op(int i) { + makewriteable(); GINAC_ASSERT(bp!=0); - return (*bp)[index]; + return bp->let_op(i); } -ex ex::operator[](int i) const +ex & ex::operator[](const ex & index) { + makewriteable(); GINAC_ASSERT(bp!=0); - return (*bp)[i]; + return (*bp)[index]; } -/** Return modifyable operand/member at position i. */ -ex & ex::let_op(int i) +ex & ex::operator[](int i) { makewriteable(); GINAC_ASSERT(bp!=0); - return bp->let_op(i); + return (*bp)[i]; } /** Left hand side of relational expression. */ diff --git a/ginac/ex.h b/ginac/ex.h index 9d592c07..3d79c7ac 100644 --- a/ginac/ex.h +++ b/ginac/ex.h @@ -159,10 +159,12 @@ public: ex symmetrize_cyclic(void) const; ex symmetrize_cyclic(const lst & l) const; ex eval_ncmul(const exvector & v) const { return bp->eval_ncmul(v); } - ex operator[](const ex & index) const; - ex operator[](int i) const; ex op(int i) const { return bp->op(i); } + ex operator[](const ex & index) const { return (*bp)[index]; } + ex operator[](int i) const { return (*bp)[i]; } ex & let_op(int i); + ex & operator[](const ex & index); + ex & operator[](int i); ex lhs(void) const; ex rhs(void) const; int compare(const ex & other) const; diff --git a/ginac/expairseq.cpp b/ginac/expairseq.cpp index 70d91ebc..228da90c 100644 --- a/ginac/expairseq.cpp +++ b/ginac/expairseq.cpp @@ -292,9 +292,9 @@ ex expairseq::op(int i) const return overall_coeff; } -ex &expairseq::let_op(int i) +ex & expairseq::let_op(int i) { - throw(std::logic_error("let_op not defined for expairseq and derived classes (add,mul,...)")); + throw(std::logic_error("let_op not defined for expairseq and derived classes (add, mul, ...)")); } ex expairseq::map(map_function &f) const diff --git a/ginac/matrix.cpp b/ginac/matrix.cpp index 49c37b10..57cd8894 100644 --- a/ginac/matrix.cpp +++ b/ginac/matrix.cpp @@ -198,12 +198,6 @@ unsigned matrix::nops() const return row*col; } -/** returns matrix entry at position (i/col, i%col). */ -ex matrix::op(int i) const -{ - return m[i]; -} - /** returns matrix entry at position (i/col, i%col). */ ex & matrix::let_op(int i) { diff --git a/ginac/matrix.h b/ginac/matrix.h index 7fabe709..5eb816bb 100644 --- a/ginac/matrix.h +++ b/ginac/matrix.h @@ -45,7 +45,6 @@ public: public: void print(const print_context & c, unsigned level = 0) const; unsigned nops() const; - ex op(int i) const; ex & let_op(int i); ex eval(int level=0) const; ex evalm(void) const {return *this;} diff --git a/ginac/power.cpp b/ginac/power.cpp index febcc4a7..718a26f7 100644 --- a/ginac/power.cpp +++ b/ginac/power.cpp @@ -233,7 +233,7 @@ unsigned power::nops() const return 2; } -ex & power::let_op(int i) +ex power::op(int i) const { GINAC_ASSERT(i>=0); GINAC_ASSERT(i<2); diff --git a/ginac/power.h b/ginac/power.h index 6759f597..7a192c41 100644 --- a/ginac/power.h +++ b/ginac/power.h @@ -52,7 +52,7 @@ public: unsigned precedence(void) const {return 60;} bool info(unsigned inf) const; unsigned nops() const; - ex & let_op(int i); + ex op(int i) const; ex map(map_function & f) const; int degree(const ex & s) const; int ldegree(const ex & s) const; diff --git a/ginac/pseries.cpp b/ginac/pseries.cpp index 1566d6ea..6077fd72 100644 --- a/ginac/pseries.cpp +++ b/ginac/pseries.cpp @@ -267,9 +267,9 @@ ex pseries::op(int i) const return seq[i].rest * power(var - point, seq[i].coeff); } -ex &pseries::let_op(int i) +ex & pseries::let_op(int i) { - throw (std::logic_error("let_op not defined for pseries")); + throw (std::logic_error("let_op() not defined for pseries")); } /** Return degree of highest power of the series. This is usually the exponent diff --git a/ginac/pseries.h b/ginac/pseries.h index a7f9037b..37ac5796 100644 --- a/ginac/pseries.h +++ b/ginac/pseries.h @@ -46,7 +46,7 @@ public: unsigned precedence(void) const {return 38;} // for clarity just below add::precedence unsigned nops(void) const; ex op(int i) const; - ex &let_op(int i); + ex & let_op(int i); int degree(const ex &s) const; int ldegree(const ex &s) const; ex coeff(const ex &s, int n = 1) const; diff --git a/ginac/relational.cpp b/ginac/relational.cpp index dc7554a6..7c395eea 100644 --- a/ginac/relational.cpp +++ b/ginac/relational.cpp @@ -165,7 +165,7 @@ unsigned relational::nops() const return 2; } -ex & relational::let_op(int i) +ex relational::op(int i) const { GINAC_ASSERT(i>=0); GINAC_ASSERT(i<2); @@ -173,6 +173,11 @@ ex & relational::let_op(int i) return i==0 ? lh : rh; } +ex relational::map(map_function & f) const +{ + return (new relational(f(lh), f(rh), o))->setflag(status_flags::dynallocated); +} + ex relational::eval(int level) const { if (level==1) diff --git a/ginac/relational.h b/ginac/relational.h index ebad1f76..133a25f3 100644 --- a/ginac/relational.h +++ b/ginac/relational.h @@ -55,7 +55,8 @@ public: unsigned precedence(void) const {return 20;} bool info(unsigned inf) const; unsigned nops() const; - ex & let_op(int i); + ex op(int i) const; + ex map(map_function & f) const; ex eval(int level=0) const; protected: