NullaryMethodExpression and UnaryFunctionExpression, derived from UnaryExpression
parent
e061143095
commit
987b123ec9
|
@ -225,22 +225,15 @@ public:
|
||||||
template<class T, class A>
|
template<class T, class A>
|
||||||
class UnaryExpression: public ExpressionNode<T> {
|
class UnaryExpression: public ExpressionNode<T> {
|
||||||
|
|
||||||
public:
|
protected:
|
||||||
|
|
||||||
typedef boost::function<T(const A&, boost::optional<Matrix&>)> function;
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
boost::shared_ptr<ExpressionNode<A> > expressionA_;
|
boost::shared_ptr<ExpressionNode<A> > expressionA_;
|
||||||
function f_;
|
|
||||||
|
|
||||||
/// Constructor with a unary function f, and input argument e
|
/// Constructor with one input argument expression
|
||||||
UnaryExpression(function f, const Expression<A>& e) :
|
UnaryExpression(const Expression<A>& e) :
|
||||||
expressionA_(e.root()), f_(f) {
|
expressionA_(e.root()) {
|
||||||
}
|
}
|
||||||
|
|
||||||
friend class Expression<T> ;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// Destructor
|
/// Destructor
|
||||||
|
@ -252,17 +245,46 @@ public:
|
||||||
return expressionA_->keys();
|
return expressionA_->keys();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
/// Nullary Method Expression
|
||||||
|
template<class T, class A>
|
||||||
|
class NullaryMethodExpression: public UnaryExpression<T, A> {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
typedef T (A::*method)(boost::optional<Matrix&>) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
method method_;
|
||||||
|
|
||||||
|
/// Constructor with a unary function f, and input argument e
|
||||||
|
NullaryMethodExpression(const Expression<A>& e, method f) :
|
||||||
|
UnaryExpression<T, A>(e), method_(f) {
|
||||||
|
}
|
||||||
|
|
||||||
|
friend class Expression<T> ;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
/// Destructor
|
||||||
|
virtual ~NullaryMethodExpression() {
|
||||||
|
}
|
||||||
|
|
||||||
/// Return value
|
/// Return value
|
||||||
virtual T value(const Values& values) const {
|
virtual T value(const Values& values) const {
|
||||||
return f_(expressionA_->value(values), boost::none);
|
using boost::none;
|
||||||
|
return (this->expressionA_->value(values).*(method_))(none);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return value and derivatives
|
/// Return value and derivatives
|
||||||
virtual Augmented<T> augmented(const Values& values) const {
|
virtual Augmented<T> augmented(const Values& values) const {
|
||||||
using boost::none;
|
using boost::none;
|
||||||
Augmented<A> argument = expressionA_->augmented(values);
|
Augmented<A> argument = this->expressionA_->augmented(values);
|
||||||
Matrix H;
|
Matrix H;
|
||||||
T t = f_(argument.value(),
|
T t = (argument.value().*(method_))(
|
||||||
argument.constant() ? none : boost::optional<Matrix&>(H));
|
argument.constant() ? none : boost::optional<Matrix&>(H));
|
||||||
return Augmented<T>(t, H, argument.jacobians());
|
return Augmented<T>(t, H, argument.jacobians());
|
||||||
}
|
}
|
||||||
|
@ -270,7 +292,50 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
/// Binary Expression
|
/// Unary Function Expression
|
||||||
|
template<class T, class A>
|
||||||
|
class UnaryFunctionExpression: public UnaryExpression<T, A> {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
typedef boost::function<T(const A&, boost::optional<Matrix&>)> function;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
function function_;
|
||||||
|
|
||||||
|
/// Constructor with a unary function f, and input argument e
|
||||||
|
UnaryFunctionExpression(function f, const Expression<A>& e) :
|
||||||
|
UnaryExpression<T, A>(e), function_(f) {
|
||||||
|
}
|
||||||
|
|
||||||
|
friend class Expression<T> ;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
/// Destructor
|
||||||
|
virtual ~UnaryFunctionExpression() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return value
|
||||||
|
virtual T value(const Values& values) const {
|
||||||
|
return function_(this->expressionA_->value(values), boost::none);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return value and derivatives
|
||||||
|
virtual Augmented<T> augmented(const Values& values) const {
|
||||||
|
using boost::none;
|
||||||
|
Augmented<A> argument = this->expressionA_->augmented(values);
|
||||||
|
Matrix H;
|
||||||
|
T t = function_(argument.value(),
|
||||||
|
argument.constant() ? none : boost::optional<Matrix&>(H));
|
||||||
|
return Augmented<T>(t, H, argument.jacobians());
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
/// Binary function Expression
|
||||||
|
|
||||||
template<class T, class A1, class A2>
|
template<class T, class A1, class A2>
|
||||||
class BinaryExpression: public ExpressionNode<T> {
|
class BinaryExpression: public ExpressionNode<T> {
|
||||||
|
@ -285,12 +350,12 @@ private:
|
||||||
|
|
||||||
boost::shared_ptr<ExpressionNode<A1> > expressionA1_;
|
boost::shared_ptr<ExpressionNode<A1> > expressionA1_;
|
||||||
boost::shared_ptr<ExpressionNode<A2> > expressionA2_;
|
boost::shared_ptr<ExpressionNode<A2> > expressionA2_;
|
||||||
function f_;
|
function function_;
|
||||||
|
|
||||||
/// Constructor with a binary function f, and two input arguments
|
/// Constructor with a binary function f, and two input arguments
|
||||||
BinaryExpression(function f, //
|
BinaryExpression(function f, //
|
||||||
const Expression<A1>& e1, const Expression<A2>& e2) :
|
const Expression<A1>& e1, const Expression<A2>& e2) :
|
||||||
expressionA1_(e1.root()), expressionA2_(e2.root()), f_(f) {
|
expressionA1_(e1.root()), expressionA2_(e2.root()), function_(f) {
|
||||||
}
|
}
|
||||||
|
|
||||||
friend class Expression<T> ;
|
friend class Expression<T> ;
|
||||||
|
@ -312,8 +377,8 @@ public:
|
||||||
/// Return value
|
/// Return value
|
||||||
virtual T value(const Values& values) const {
|
virtual T value(const Values& values) const {
|
||||||
using boost::none;
|
using boost::none;
|
||||||
return f_(expressionA1_->value(values), expressionA2_->value(values), none,
|
return function_(expressionA1_->value(values), expressionA2_->value(values),
|
||||||
none);
|
none, none);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return value and derivatives
|
/// Return value and derivatives
|
||||||
|
@ -322,7 +387,7 @@ public:
|
||||||
Augmented<A1> argument1 = expressionA1_->augmented(values);
|
Augmented<A1> argument1 = expressionA1_->augmented(values);
|
||||||
Augmented<A2> argument2 = expressionA2_->augmented(values);
|
Augmented<A2> argument2 = expressionA2_->augmented(values);
|
||||||
Matrix H1, H2;
|
Matrix H1, H2;
|
||||||
T t = f_(argument1.value(), argument2.value(),
|
T t = function_(argument1.value(), argument2.value(),
|
||||||
argument1.constant() ? none : boost::optional<Matrix&>(H1),
|
argument1.constant() ? none : boost::optional<Matrix&>(H1),
|
||||||
argument2.constant() ? none : boost::optional<Matrix&>(H2));
|
argument2.constant() ? none : boost::optional<Matrix&>(H2));
|
||||||
return Augmented<T>(t, H1, argument1.jacobians(), H2, argument2.jacobians());
|
return Augmented<T>(t, H1, argument1.jacobians(), H2, argument2.jacobians());
|
||||||
|
@ -345,11 +410,11 @@ private:
|
||||||
|
|
||||||
boost::shared_ptr<ExpressionNode<A1> > expressionA1_;
|
boost::shared_ptr<ExpressionNode<A1> > expressionA1_;
|
||||||
boost::shared_ptr<ExpressionNode<A2> > expressionA2_;
|
boost::shared_ptr<ExpressionNode<A2> > expressionA2_;
|
||||||
method f_;
|
method method_;
|
||||||
|
|
||||||
/// Constructor with a binary function f, and two input arguments
|
/// Constructor with a binary function f, and two input arguments
|
||||||
MethodExpression(const Expression<A1>& e1, method f, const Expression<A2>& e2) :
|
MethodExpression(const Expression<A1>& e1, method f, const Expression<A2>& e2) :
|
||||||
expressionA1_(e1.root()), expressionA2_(e2.root()), f_(f) {
|
expressionA1_(e1.root()), expressionA2_(e2.root()), method_(f) {
|
||||||
}
|
}
|
||||||
|
|
||||||
friend class Expression<T> ;
|
friend class Expression<T> ;
|
||||||
|
@ -371,7 +436,7 @@ public:
|
||||||
/// Return value
|
/// Return value
|
||||||
virtual T value(const Values& values) const {
|
virtual T value(const Values& values) const {
|
||||||
using boost::none;
|
using boost::none;
|
||||||
return (expressionA1_->value(values).*(f_))(expressionA2_->value(values),
|
return (expressionA1_->value(values).*(method_))(expressionA2_->value(values),
|
||||||
none, none);
|
none, none);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -381,7 +446,7 @@ public:
|
||||||
Augmented<A1> argument1 = expressionA1_->augmented(values);
|
Augmented<A1> argument1 = expressionA1_->augmented(values);
|
||||||
Augmented<A2> argument2 = expressionA2_->augmented(values);
|
Augmented<A2> argument2 = expressionA2_->augmented(values);
|
||||||
Matrix H1, H2;
|
Matrix H1, H2;
|
||||||
T t = (argument1.value().*(f_))(argument2.value(),
|
T t = (argument1.value().*(method_))(argument2.value(),
|
||||||
argument1.constant() ? none : boost::optional<Matrix&>(H1),
|
argument1.constant() ? none : boost::optional<Matrix&>(H1),
|
||||||
argument2.constant() ? none : boost::optional<Matrix&>(H2));
|
argument2.constant() ? none : boost::optional<Matrix&>(H2));
|
||||||
return Augmented<T>(t, H1, argument1.jacobians(), H2, argument2.jacobians());
|
return Augmented<T>(t, H1, argument1.jacobians(), H2, argument2.jacobians());
|
||||||
|
|
|
@ -52,12 +52,20 @@ public:
|
||||||
root_(new LeafExpression<T>(Symbol(c, j))) {
|
root_(new LeafExpression<T>(Symbol(c, j))) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a unary expression
|
/// Construct a nullary method expression
|
||||||
template<typename E>
|
template<typename E>
|
||||||
Expression(typename UnaryExpression<T, E>::function f,
|
Expression(const Expression<E>& expression,
|
||||||
|
typename NullaryMethodExpression<T, E>::method f) {
|
||||||
|
// TODO Assert that root of expression is not null.
|
||||||
|
root_.reset(new NullaryMethodExpression<T, E>(expression, f));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Construct a unary function expression
|
||||||
|
template<typename E>
|
||||||
|
Expression(typename UnaryFunctionExpression<T, E>::function f,
|
||||||
const Expression<E>& expression) {
|
const Expression<E>& expression) {
|
||||||
// TODO Assert that root of expression is not null.
|
// TODO Assert that root of expression is not null.
|
||||||
root_.reset(new UnaryExpression<T, E>(f, expression));
|
root_.reset(new UnaryFunctionExpression<T, E>(f, expression));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a binary expression
|
/// Construct a binary expression
|
||||||
|
|
|
@ -62,6 +62,20 @@ TEST(Expression, leaf) {
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
|
|
||||||
|
TEST(Expression, nullaryMethod) {
|
||||||
|
Expression<Point3> p(67);
|
||||||
|
Expression<double> norm(p, &Point3::norm);
|
||||||
|
Values values;
|
||||||
|
values.insert(67,Point3(3,4,5));
|
||||||
|
Augmented<double> a = norm.augmented(values);
|
||||||
|
EXPECT(a.value() == sqrt(50));
|
||||||
|
JacobianMap expected;
|
||||||
|
expected[67] = (Matrix(1,3) << 3/sqrt(50),4/sqrt(50),5/sqrt(50));
|
||||||
|
EXPECT(assert_equal(expected.at(67),a.jacobians().at(67)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************* */
|
||||||
|
|
||||||
TEST(Expression, test) {
|
TEST(Expression, test) {
|
||||||
|
|
||||||
// Test Constant expression
|
// Test Constant expression
|
||||||
|
|
Loading…
Reference in New Issue