LightJason - AgentSpeak(L++)
TestCActionMathInterpolate.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.action.builtin;
25 
26 import org.apache.commons.math3.analysis.interpolation.LinearInterpolator;
27 import org.apache.commons.math3.analysis.interpolation.NevilleInterpolator;
28 import org.apache.commons.math3.analysis.polynomials.PolynomialFunctionLagrangeForm;
29 import org.apache.commons.math3.analysis.polynomials.PolynomialFunctionNewtonForm;
30 import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
31 import org.junit.Assert;
32 import org.junit.Test;
40 
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.stream.Collectors;
44 import java.util.stream.Stream;
45 
46 import static org.junit.Assert.assertTrue;
47 
51 public final class TestCActionMathInterpolate extends IBaseTest
52 {
53 
57  @Test
58  public final void create()
59  {
60  final List<ITerm> l_return = new ArrayList<>();
61 
62  new CCreate().execute(
63  false, IContext.EMPTYPLAN,
64  Stream.of( "linear", 2, 3, 8, 11, 13, 20 ).map( CRawTerm::from ).collect( Collectors.toList() ),
65  l_return
66  );
67 
68  new CCreate().execute(
69  false, IContext.EMPTYPLAN,
70  Stream.of( "divideddifference", 2, 3, 8, 11, 13, 20 ).map( CRawTerm::from ).collect( Collectors.toList() ),
71  l_return
72  );
73 
74  new CCreate().execute(
75  false, IContext.EMPTYPLAN,
76  Stream.of( "neville", 2, 3, 8, 11, 13, 20 ).map( CRawTerm::from ).collect( Collectors.toList() ),
77  l_return
78  );
79 
80  new CCreate().execute(
81  false, IContext.EMPTYPLAN,
82  Stream.of( "akima", 42, 65, 78, 87, 100, 150, 41, 63, 82, 98, 110, 200 ).map( CRawTerm::from ).collect( Collectors.toList() ),
83  l_return
84  );
85 
86  new CCreate().execute(
87  false, IContext.EMPTYPLAN,
88  Stream.of( "loess", 42, 65, 78, 87, 100, 150, 300, 400, 500, 41, 63, 82, 98, 110, 200, 400, 600, 800 ).map( CRawTerm::from ).collect( Collectors.toList() ),
89  l_return
90  );
91 
92  Assert.assertEquals( l_return.size(), 5 );
93  assertTrue( l_return.get( 0 ).raw() instanceof PolynomialSplineFunction );
94  assertTrue( l_return.get( 1 ).raw() instanceof PolynomialFunctionNewtonForm );
95  assertTrue( l_return.get( 2 ).raw() instanceof PolynomialFunctionLagrangeForm );
96  assertTrue( l_return.get( 3 ).raw() instanceof PolynomialSplineFunction );
97  assertTrue( l_return.get( 4 ).raw() instanceof PolynomialSplineFunction );
98  }
99 
103  @Test
104  public final void singleinterpolate()
105  {
106  final List<ITerm> l_return = new ArrayList<>();
107 
109  false, IContext.EMPTYPLAN,
110  Stream.of(
111  new LinearInterpolator().interpolate( new double[]{3, 6}, new double[]{11, 13} ), 3, 4
112  ).map( CRawTerm::from ).collect( Collectors.toList() ),
113  l_return
114  );
115  Assert.assertEquals( l_return.size(), 2 );
116  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 11.0 );
117  Assert.assertEquals( l_return.get( 1 ).<Number>raw(), 11.666666666666666 );
118  }
119 
123  @Test
124  public final void multipleinterpolate()
125  {
126  final List<ITerm> l_return = new ArrayList<>();
127 
129  false, IContext.EMPTYPLAN,
130  Stream.of(
131  5,
132  new LinearInterpolator().interpolate( new double[]{3, 6}, new double[]{11, 13} ),
133  new NevilleInterpolator().interpolate( new double[]{2, 3, 8}, new double[]{11, 13, 20} )
134  ).map( CRawTerm::from ).collect( Collectors.toList() ),
135  l_return
136  );
137 
138  Assert.assertEquals( l_return.size(), 2 );
139  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 12.333333333333334 );
140  Assert.assertEquals( l_return.get( 1 ).<Number>raw(), 16.400000000000002 );
141  }
142 
143 }
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
base test class with helpers
Definition: IBaseTest.java:33
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
execution context with local data
Definition: IContext.java:42
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
term structure for simple datatypes
Definition: CRawTerm.java:45