You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Luke Lindsay <lu...@gmail.com> on 2014/10/03 13:40:20 UTC
[math] DerivativeStructure constants
Hello,
I am using the apache math DerivativeStructure framework. In general
I find it very useful, however sometimes I find having to specify the
number of free parameters and order for what are essentially constants
makes code complex.
I often find myself writing code similar to the following.
public DerivativeStructure sum(DerivativeStructure[] values){
DerivativeStructure sum = new
DerivativeStructure(values[0].getFreeParameters(),
values[0].getOrder());
for (int i = 0; i < values.length; i++) {
sum = sum.add(values[i]);
}
return sum;
}
Or
public DerivativeStructure sum(DerivativeStructure[] values, int
params, int order){
DerivativeStructure sum = new DerivativeStructure(params, order);
for (int i = 0; i < values.length; i++) {
sum = sum.add(values[i]);
}
return sum;
}
I would prefer not to have to worry about getting access to the number
of free parameters and order, and to be able to write something like
the following.
public DerivativeStructure sum(DerivativeStructure[] values){
DerivativeStructure sum = DerivativeStructure.constant(0);
//Where DerivativeStructure.constant(x) is equivalent to new
DerivativeStructure(0,0,x);
for (int i = 0; i < values.length; i++) {
sum = sum.add(values[i]);
}
return sum;
}
But if I use DerivativeStructure sum = new
DerivativeStructure(0,0,0), a DimensionMismatchException gets thrown.
Is there a way to avoid the complexities of the code in the first two
examples above?
Luke
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [math] DerivativeStructure constants
Posted by Luc Maisonobe <lu...@spaceroots.org>.
Hi Luke,
Le 03/10/2014 13:40, Luke Lindsay a écrit :
> Hello,
>
> I am using the apache math DerivativeStructure framework. In general
> I find it very useful, however sometimes I find having to specify the
> number of free parameters and order for what are essentially constants
> makes code complex.
Thanks for using it.
>
> I often find myself writing code similar to the following.
>
> public DerivativeStructure sum(DerivativeStructure[] values){
> DerivativeStructure sum = new
> DerivativeStructure(values[0].getFreeParameters(),
> values[0].getOrder());
> for (int i = 0; i < values.length; i++) {
> sum = sum.add(values[i]);
> }
> return sum;
> }
>
> Or
>
> public DerivativeStructure sum(DerivativeStructure[] values, int
> params, int order){
> DerivativeStructure sum = new DerivativeStructure(params, order);
> for (int i = 0; i < values.length; i++) {
> sum = sum.add(values[i]);
> }
> return sum;
> }
>
> I would prefer not to have to worry about getting access to the number
> of free parameters and order, and to be able to write something like
> the following.
>
> public DerivativeStructure sum(DerivativeStructure[] values){
> DerivativeStructure sum = DerivativeStructure.constant(0);
> //Where DerivativeStructure.constant(x) is equivalent to new
> DerivativeStructure(0,0,x);
> for (int i = 0; i < values.length; i++) {
> sum = sum.add(values[i]);
> }
> return sum;
> }
>
> But if I use DerivativeStructure sum = new
> DerivativeStructure(0,0,0), a DimensionMismatchException gets thrown.
>
> Is there a way to avoid the complexities of the code in the first two
> examples above?
There is one way, but it is only a slight simplification. You can do this:
public DerivativeStructure sum(DerivativeStructure[] values){
DerivativeStructure sum = values[0].getField().getZero();
for (int i = 0; i < values.length; i++) {
sum = sum.add(values[i]);
}
return sum;
}
All field instances provide a getZero() and a getOne() method, so
you can do a similar trick for initializing a multiplicative operation too.
best regards,
Luc
> Luke
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org