Skip to content

Commit e91021f

Browse files
jmirabelolivier-stasse
authored andcommitted
Make Add_of_(matrix,vector,double) variadic.
1 parent c87372d commit e91021f

File tree

1 file changed

+66
-43
lines changed

1 file changed

+66
-43
lines changed

src/matrix/operator.cpp

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -616,49 +616,6 @@ namespace dynamicgraph {
616616
namespace dynamicgraph {
617617
namespace sot {
618618

619-
/* --- ADDITION --------------------------------------------------------- */
620-
template< typename T>
621-
struct Adder
622-
: public BinaryOpHeader<T,T,T>
623-
{
624-
double coeff1, coeff2;
625-
Adder () : coeff1 (1.), coeff2 (1.) {}
626-
void operator()( const T& v1,const T& v2,T& res ) const
627-
{
628-
res=coeff1*v1; res+=coeff2*v2;
629-
}
630-
631-
void addSpecificCommands(Entity& ent,
632-
Entity::CommandMap_t& commandMap )
633-
{
634-
using namespace dynamicgraph::command;
635-
std::string doc;
636-
637-
ADD_COMMAND( "setCoeff1",
638-
makeDirectSetter(ent,&coeff1,docDirectSetter("coeff1","double")));
639-
ADD_COMMAND( "setCoeff2",
640-
makeDirectSetter(ent,&coeff2,docDirectSetter("coeff2","double")));
641-
}
642-
virtual std::string getDocString () const
643-
{
644-
return std::string
645-
("Linear combination of inputs\n"
646-
" - input ") + BinaryOpHeader<T,T,T>::nameTypeIn1 () +
647-
std::string ("\n"
648-
" - ") + BinaryOpHeader<T,T,T>::nameTypeIn2 () +
649-
std::string ("\n"
650-
" - output ") + BinaryOpHeader<T,T,T>::nameTypeOut () +
651-
std::string ("\n"" sout = coeff1 * sin1 + coeff2 * sin2\n"
652-
" Coefficients are set by commands, default value is 1.\n");
653-
}
654-
};
655-
656-
657-
REGISTER_BINARY_OP(Adder<dynamicgraph::Matrix>,Add_of_matrix);
658-
REGISTER_BINARY_OP(Adder<dynamicgraph::Vector>,Add_of_vector);
659-
REGISTER_BINARY_OP(Adder<double>,Add_of_double);
660-
661-
662619
/* --- MULTIPLICATION --------------------------------------------------- */
663620

664621
template< typename F,typename E>
@@ -991,6 +948,72 @@ namespace dynamicgraph {
991948
};
992949
REGISTER_VARIADIC_OP(VectorMix,Mix_of_vector);
993950

951+
/* --- ADDITION --------------------------------------------------------- */
952+
template< typename T>
953+
struct AdderVariadic
954+
: public VariadicOpHeader<T,T>
955+
{
956+
typedef VariadicOp<AdderVariadic> Base;
957+
958+
Base* entity;
959+
Vector coeffs;
960+
961+
AdderVariadic () : coeffs () {}
962+
void operator()( const std::vector<const T*>& vs, T& res ) const
963+
{
964+
assert (vs.size() == coeffs.size());
965+
if (vs.size() == 0) return;
966+
res = coeffs[0]*(*vs[0]);
967+
for (std::size_t i = 1; i < vs.size(); ++i)
968+
res+=coeffs[i]*(*vs[i]);
969+
}
970+
971+
void setCoeffs(const Vector& c) { coeffs = c; }
972+
void setSignalNumber (const int& n)
973+
{
974+
coeffs = Vector::Ones(n);
975+
entity->setSignalNumber(n);
976+
}
977+
978+
void initialize(Base* ent,
979+
Entity::CommandMap_t& commandMap )
980+
{
981+
using namespace dynamicgraph::command;
982+
entity = ent;
983+
984+
setSignalNumber (2);
985+
986+
commandMap.insert(std::make_pair( "getSignalNumber",
987+
new Getter<Base, int> (*ent, &Base::getSignalNumber,
988+
"Get the number of input vector.")));
989+
990+
commandMap.insert(std::make_pair("setSignalNumber",
991+
makeCommandVoid1<Base,int>(*ent,
992+
boost::function <void (const int&)> (boost::bind ( &AdderVariadic::setSignalNumber, this, _1)),
993+
docCommandVoid1("set the number of input vector.", "int (size)"))));
994+
995+
commandMap.insert(std::make_pair("setCoeffs",
996+
makeCommandVoid1<Base,Vector>(*ent,
997+
boost::function <void (const Vector&)> (boost::bind ( &AdderVariadic::setCoeffs, this, _1)),
998+
docCommandVoid1("set the multipliers.", "vector"))));
999+
}
1000+
1001+
virtual std::string getDocString () const
1002+
{
1003+
return
1004+
"Linear combination of inputs\n"
1005+
" - input " + VariadicOpHeader<T,T>::nameTypeIn () +
1006+
"\n"
1007+
" - output " + VariadicOpHeader<T,T>::nameTypeOut () +
1008+
"\n"
1009+
" sout = sum ([coeffs[i] * sin[i] for i in range(n) ])\n"
1010+
" Coefficients are set by commands, default value is 1.\n";
1011+
}
1012+
};
1013+
REGISTER_VARIADIC_OP(AdderVariadic<Matrix>,Add_of_matrix);
1014+
REGISTER_VARIADIC_OP(AdderVariadic<Vector>,Add_of_vector);
1015+
REGISTER_VARIADIC_OP(AdderVariadic<double>,Add_of_double);
1016+
9941017
/* --- MULTIPLICATION --------------------------------------------------- */
9951018
template< typename T>
9961019
struct Multiplier

0 commit comments

Comments
 (0)