LightJason - AgentSpeak(L++)
TestCActionMathBlas.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 cern.colt.matrix.tdouble.DoubleMatrix1D;
27 import cern.colt.matrix.tdouble.DoubleMatrix2D;
28 import cern.colt.matrix.tdouble.impl.DenseDoubleMatrix1D;
29 import cern.colt.matrix.tdouble.impl.DenseDoubleMatrix2D;
30 import org.junit.Assert;
31 import org.junit.Before;
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 
49 public final class TestCActionMathBlas extends IBaseTest
50 {
54  private DoubleMatrix2D m_matrix1;
55 
59  private DoubleMatrix2D m_matrix2;
60 
64  private DoubleMatrix1D m_vector;
65 
66 
70  @Before
71  public final void initialize()
72  {
73  m_matrix1 = new DenseDoubleMatrix2D( new double[][]{{2, 6}, {3, 8}} );
74  m_matrix2 = new DenseDoubleMatrix2D( new double[][]{{2, 2}, {3, 1}} );
75  m_vector = new DenseDoubleMatrix1D( new double[]{2, 5} );
76  }
77 
81  @Test
82  public final void size()
83  {
84  final List<ITerm> l_return = new ArrayList<>();
85 
86  new CSize().execute(
87  false, IContext.EMPTYPLAN,
88  Stream.of( m_matrix1 ).map( CRawTerm::from ).collect( Collectors.toList() ),
89  l_return
90  );
91 
92  Assert.assertEquals( l_return.size(), 1 );
93  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 4D );
94  }
95 
99  @Test
100  public final void multiply()
101  {
102  final List<ITerm> l_return = new ArrayList<>();
103 
104  new CMultiply().execute(
105  false, IContext.EMPTYPLAN,
106  Stream.of( m_matrix1, m_matrix2, m_matrix1, m_vector, m_vector, m_vector, m_vector, m_matrix2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
107  l_return
108  );
109 
110  Assert.assertEquals( l_return.size(), 4 );
111  Assert.assertArrayEquals( l_return.get( 0 ).<DoubleMatrix2D>raw().toArray(), new double[][]{{22.0, 10.0}, {30.0, 14.0}} );
112  Assert.assertArrayEquals( l_return.get( 1 ).<DoubleMatrix1D>raw().toArray(), new double[]{34.0, 46.0}, 0 );
113  Assert.assertArrayEquals( l_return.get( 2 ).<DoubleMatrix2D>raw().toArray(), new double[][]{{4.0, 10.0}, {10.0, 25.0}} );
114  Assert.assertArrayEquals( l_return.get( 3 ).<DoubleMatrix1D>raw().toArray(), new double[]{14.0, 11.0}, 0 );
115  }
116 
120  @Test
121  public final void elementwisematrix()
122  {
123  final List<ITerm> l_return = new ArrayList<>();
124 
125  new CElementWise().execute(
126  false, IContext.EMPTYPLAN,
127  Stream.of(
128  m_matrix1, "+", m_matrix2,
129  m_matrix1, "+", 5,
130  m_matrix1, "-", 5,
131  m_matrix1, "*", 5,
132  m_matrix1, "/", 2,
133  m_matrix1, "|+|", m_matrix2,
134  m_matrix1, "-", m_matrix2,
135  m_matrix1, "*", m_matrix2,
136  m_matrix1, "|+|", -9
137  ).map( CRawTerm::from ).collect( Collectors.toList() ),
138  l_return
139  );
140 
141  Assert.assertEquals( l_return.size(), 9 );
142  Assert.assertArrayEquals( l_return.get( 0 ).<DoubleMatrix2D>raw().toArray(), new double[][]{{4.0, 8.0}, {6.0, 9.0}} );
143  Assert.assertArrayEquals( l_return.get( 1 ).<DoubleMatrix2D>raw().toArray(), new double[][]{{7.0, 11.0}, {8.0, 13.0}} );
144  Assert.assertArrayEquals( l_return.get( 2 ).<DoubleMatrix2D>raw().toArray(), new double[][]{{-3.0, 1.0}, {-2.0, 3.0}} );
145  Assert.assertArrayEquals( l_return.get( 3 ).<DoubleMatrix2D>raw().toArray(), new double[][]{{10.0, 30.0}, {15.0, 40.0}} );
146  Assert.assertArrayEquals( l_return.get( 4 ).<DoubleMatrix2D>raw().toArray(), new double[][]{{1.0, 3.0}, {1.5, 4.0}} );
147  Assert.assertArrayEquals( l_return.get( 5 ).<DoubleMatrix2D>raw().toArray(), new double[][]{{4.0, 8.0}, {6.0, 9.0}} );
148  Assert.assertArrayEquals( l_return.get( 6 ).<DoubleMatrix2D>raw().toArray(), new double[][]{{0.0, 4.0}, {0.0, 7.0}} );
149  Assert.assertArrayEquals( l_return.get( 7 ).<DoubleMatrix2D>raw().toArray(), new double[][]{{4.0, 12.0}, {9.0, 8.0}} );
150  Assert.assertArrayEquals( l_return.get( 8 ).<DoubleMatrix2D>raw().toArray(), new double[][]{{7.0, 3.0}, {6.0, 1.0}} );
151 
152  }
153 
157  @Test
158  public final void elementwisevector()
159  {
160  final List<ITerm> l_return = new ArrayList<>();
161 
162  new CElementWise().execute(
163  false, IContext.EMPTYPLAN,
164  Stream.of(
165  m_vector, "+", m_vector,
166  m_vector, "+", 5,
167  m_vector, "-", 5,
168  m_vector, "*", 5,
169  m_vector, "/", 2,
170  m_vector, "-", m_vector,
171  m_vector, "*", m_vector,
172  m_vector, "|+|", -5
173  ).map( CRawTerm::from ).collect( Collectors.toList() ),
174  l_return
175  );
176 
177  Assert.assertEquals( l_return.size(), 8 );
178  Assert.assertArrayEquals( l_return.get( 0 ).<DoubleMatrix1D>raw().toArray(), new double[]{4.0, 10.0}, 0 );
179  Assert.assertArrayEquals( l_return.get( 1 ).<DoubleMatrix1D>raw().toArray(), new double[]{7.0, 10.0}, 0 );
180  Assert.assertArrayEquals( l_return.get( 2 ).<DoubleMatrix1D>raw().toArray(), new double[]{-3.0, 0.0}, 0 );
181  Assert.assertArrayEquals( l_return.get( 3 ).<DoubleMatrix1D>raw().toArray(), new double[]{10.0, 25.0}, 0 );
182  Assert.assertArrayEquals( l_return.get( 4 ).<DoubleMatrix1D>raw().toArray(), new double[]{1.0, 2.5}, 0 );
183  Assert.assertArrayEquals( l_return.get( 5 ).<DoubleMatrix1D>raw().toArray(), new double[]{0.0, 0.0}, 0 );
184  Assert.assertArrayEquals( l_return.get( 6 ).<DoubleMatrix1D>raw().toArray(), new double[]{4.0, 25.0}, 0 );
185  Assert.assertArrayEquals( l_return.get( 7 ).<DoubleMatrix1D>raw().toArray(), new double[]{3.0, 0.0}, 0 );
186 
187  }
188 
189 }
base test class with helpers
Definition: IBaseTest.java:33
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
returns the elements within the BLAS structure.
execution context with local data
Definition: IContext.java:42
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
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
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
Definition: CMultiply.java:74
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
term structure for simple datatypes
Definition: CRawTerm.java:45