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: [email protected]
> For additional commands, e-mail: [email protected]
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]