24 package org.lightjason.agentspeak.action.builtin;
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 cern.colt.matrix.tdouble.impl.SparseDoubleMatrix2D;
31 import com.codepoetics.protonpack.StreamUtils;
32 import com.tngtech.java.junit.dataprovider.DataProvider;
33 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
34 import com.tngtech.java.junit.dataprovider.UseDataProvider;
35 import org.apache.commons.lang3.tuple.ImmutableTriple;
36 import org.apache.commons.lang3.tuple.Triple;
37 import org.junit.Assert;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
81 import java.lang.reflect.InvocationTargetException;
82 import java.util.ArrayList;
83 import java.util.Arrays;
84 import java.util.Collections;
85 import java.util.List;
86 import java.util.Random;
87 import java.util.stream.Collectors;
88 import java.util.stream.IntStream;
89 import java.util.stream.Stream;
91 import static org.junit.Assert.assertTrue;
96 @RunWith( DataProviderRunner.class )
104 private static final DoubleMatrix2D MATRIX1 =
new DenseDoubleMatrix2D(
new double[][]{{2, 6}, {3, 8}} );
110 private static final DoubleMatrix2D MATRIX2 =
new DenseDoubleMatrix2D(
new double[][]{{2, 2}, {3, 1}} );
121 Stream.of( MATRIX1, MATRIX2 ),
139 Stream.of( 2D, 2D, 2D, 2D ),
142 Stream.of( 56.48229533707812, 4.265564437074639 ),
143 Stream.of( -2.000000000000001, -4.0 ),
144 Stream.of( 10.628480167651258, 4.130648586880582 ),
145 Stream.of( 14.0000, 5.0000 ),
146 Stream.of( 10.63014581273465, 4.242640687119285 ),
147 Stream.of( 11.0000, 4.0000 ),
149 Stream.of( MATRIX1.zSum(), MATRIX2.zSum() ),
150 Stream.of( 10.0, 3.0 )
165 @SuppressWarnings(
"varargs" )
166 private static Stream<Object> testcase( final Stream<Object> p_input, final Stream<Class<?>> p_classes, final Stream<Object>... p_classresult )
168 final List<ITerm> l_input = p_input.map(
CRawTerm::from ).collect( Collectors.toList() );
170 return StreamUtils.zip(
172 Arrays.stream( p_classresult ),
173 ( i, j ) ->
new ImmutableTriple<>( l_input, i, j )
188 @UseDataProvider(
"generator" )
189 public final
void action( final Triple<List<
ITerm>, Class<? extends
IAction>, Stream<Object>> p_input )
190 throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException
192 final List<ITerm> l_return =
new ArrayList<>();
194 p_input.getMiddle().getConstructor().newInstance().execute(
200 Assert.assertArrayEquals(
201 p_input.getMiddle().toGenericString(),
202 l_return.stream().map(
ITerm::raw ).toArray(),
203 p_input.getRight().toArray()
213 final List<ITerm> l_return =
new ArrayList<>();
218 Stream.of( 2, 2,
"dense" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
222 Assert.assertEquals( l_return.size(), 1 );
223 assertTrue( l_return.get( 0 ).raw() instanceof DoubleMatrix2D );
224 Assert.assertEquals( l_return.get( 0 ).<DoubleMatrix2D>raw().size(), 4 );
225 Assert.assertEquals( l_return.get( 0 ).<DoubleMatrix2D>raw().rows(), 2 );
226 Assert.assertEquals( l_return.get( 0 ).<DoubleMatrix2D>raw().columns(), 2 );
235 final List<ITerm> l_return =
new ArrayList<>();
239 Stream.of( 1, MATRIX2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
243 Assert.assertEquals( l_return.size(), 1 );
244 Assert.assertTrue( l_return.get( 0 ).raw() instanceof DoubleMatrix1D );
245 Assert.assertEquals( l_return.get( 0 ).<DoubleMatrix1D>raw().size(), 2 );
247 Assert.assertEquals( l_return.get( 0 ).<DoubleMatrix1D>raw().get( 0 ), 2, 0 );
248 Assert.assertEquals( l_return.get( 0 ).<DoubleMatrix1D>raw().get( 1 ), 1, 0 );
257 final List<ITerm> l_return =
new ArrayList<>();
261 Stream.of( 1, MATRIX2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
265 Assert.assertEquals( l_return.size(), 1 );
266 Assert.assertTrue( l_return.get( 0 ).raw() instanceof DoubleMatrix1D );
267 Assert.assertEquals( l_return.get( 0 ).<DoubleMatrix1D>raw().size(), 2 );
269 Assert.assertEquals( l_return.get( 0 ).<DoubleMatrix1D>raw().get( 0 ), 3, 0 );
270 Assert.assertEquals( l_return.get( 0 ).<DoubleMatrix1D>raw().get( 1 ), 1, 0 );
279 final List<ITerm> l_return =
new ArrayList<>();
283 Stream.of( 2, MATRIX2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
287 Assert.assertEquals( l_return.size(), 1 );
288 Assert.assertTrue( l_return.get( 0 ).raw() instanceof DoubleMatrix2D );
289 Assert.assertEquals( l_return.get( 0 ).<DoubleMatrix2D>raw().size(), 4 );
291 Assert.assertArrayEquals(
292 l_return.get( 0 ).<DoubleMatrix2D>raw().toArray(),
293 new DenseDoubleMatrix2D(
new double[][]{{10.0, 6.0}, {9.0, 7.0}} ).toArray()
302 public final void set()
306 Stream.of( 0, 1, 6.0, MATRIX1 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
307 Collections.emptyList()
310 Assert.assertEquals( MATRIX1.get( 0, 1 ), 6, 0 );
319 final List<ITerm> l_return =
new ArrayList<>();
323 Stream.of( MATRIX1 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
327 Assert.assertEquals( l_return.size(), 1 );
328 Assert.assertTrue( l_return.get( 0 ).raw() instanceof List );
330 Assert.assertArrayEquals(
331 l_return.get( 0 ).<List<?>>raw().toArray(),
332 Stream.of( 2.0, 6.0, 3.0, 8.0 ).collect( Collectors.toList() ).toArray()
342 final List<ITerm> l_return =
new ArrayList<>();
346 Stream.of( MATRIX2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
350 Assert.assertEquals( l_return.size(), 1 );
351 Assert.assertTrue( l_return.get( 0 ).raw() instanceof DoubleMatrix2D );
353 Assert.assertArrayEquals(
354 l_return.get( 0 ).<DoubleMatrix2D>raw().toArray(),
355 new DenseDoubleMatrix2D(
new double[][]{{2.0, 3.0}, {2.0, 1.0}} ).toArray()
365 final List<ITerm> l_return =
new ArrayList<>();
369 Stream.of( 0, 0, 0, 1, MATRIX2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
373 Assert.assertEquals( l_return.size(), 1 );
374 Assert.assertTrue( l_return.get( 0 ).raw() instanceof DoubleMatrix2D );
376 Assert.assertArrayEquals(
377 l_return.get( 0 ).<DoubleMatrix2D>raw().toArray(),
378 new DenseDoubleMatrix2D(
new double[][]{{2.0, 2.0}} ).toArray()
388 final List<ITerm> l_return =
new ArrayList<>();
392 Stream.of( MATRIX1, MATRIX2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
396 Assert.assertEquals( l_return.size(), 1 );
397 Assert.assertTrue( l_return.get( 0 ).raw() instanceof DoubleMatrix2D );
399 Assert.assertArrayEquals(
400 l_return.get( 0 ).<DoubleMatrix2D>raw().toArray(),
401 new DenseDoubleMatrix2D(
new double[][]{{1.0, -4.999999999999998}, {0.0, 1.9999999999999993}} ).toArray()
411 final DoubleMatrix2D l_matrix =
new DenseDoubleMatrix2D( 2, 2 );
415 Stream.of( 2, l_matrix ).map(
CRawTerm::from ).collect( Collectors.toList() ),
416 Collections.emptyList()
419 Assert.assertArrayEquals( l_matrix.toArray(),
new double[][]{{2.0, 2.0}, {2.0, 2.0}} );
428 final DoubleMatrix2D l_matrix =
new DenseDoubleMatrix2D( 2, 2 );
432 Stream.of( MATRIX2, l_matrix ).map(
CRawTerm::from ).collect( Collectors.toList() ),
433 Collections.emptyList()
436 Assert.assertArrayEquals( MATRIX2.toArray(), l_matrix.toArray() );
443 public final void get()
445 final List<ITerm> l_return =
new ArrayList<>();
449 Stream.of( MATRIX2, 0, 1 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
453 Assert.assertEquals( l_return.size(), 1 );
454 Assert.assertTrue( l_return.get( 0 ).raw() instanceof Double );
455 Assert.assertEquals( l_return.get( 0 ).<Double>raw(), 2, 0 );
464 final List<ITerm> l_return =
new ArrayList<>();
468 Stream.of(
"1,2;3,4",
"dense" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
472 Assert.assertEquals( l_return.size(), 1 );
473 Assert.assertTrue( l_return.get( 0 ).raw() instanceof DoubleMatrix2D );
474 Assert.assertArrayEquals(
475 l_return.get( 0 ).<DoubleMatrix2D>raw().toArray(),
476 new DenseDoubleMatrix2D(
new double[][]{{1.0, 2.0}, {3.0, 4.0}} ).toArray()
486 final List<ITerm> l_return =
new ArrayList<>();
490 Stream.of( MATRIX2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
494 Assert.assertEquals( l_return.size(), 1 );
495 Assert.assertTrue( l_return.get( 0 ).raw() instanceof DoubleMatrix2D );
496 Assert.assertArrayEquals(
497 l_return.get( 0 ).<DoubleMatrix2D>raw().toArray(),
498 new DenseDoubleMatrix2D(
new double[][]{{-0.24999999999999994, 0.5}, {0.7499999999999999, -0.4999999999999999}} ).toArray()
508 final List<ITerm> l_return =
new ArrayList<>();
512 Stream.of( MATRIX2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
516 Assert.assertEquals( l_return.size(), 2 );
517 Assert.assertTrue( l_return.get( 0 ).raw() instanceof DenseDoubleMatrix1D );
518 Assert.assertArrayEquals( l_return.get( 0 ).<DenseDoubleMatrix1D>raw().toArray(), Stream.of( 4, -1 ).mapToDouble( i -> i ).toArray(), 0 );
520 Assert.assertTrue( l_return.get( 1 ).raw() instanceof DoubleMatrix2D );
521 Assert.assertArrayEquals(
522 l_return.get( 1 ).<DoubleMatrix2D>raw().toArray(),
523 new DenseDoubleMatrix2D(
new double[][]{{0.7071067811865475, -0.565685424949238}, {0.7071067811865475, 0.8485281374238569}} ).toArray()
533 final List<ITerm> l_return =
new ArrayList<>();
537 Stream.of( MATRIX2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
541 Assert.assertEquals( l_return.size(), 3 );
542 Assert.assertTrue( l_return.get( 0 ).raw() instanceof DenseDoubleMatrix1D );
543 Assert.assertArrayEquals( l_return.get( 0 ).<DenseDoubleMatrix1D>raw().toArray(),
544 Stream.of( 4.130648586880582, 0.9683709267122025 ).mapToDouble( i -> i ).toArray(), 0 );
546 Assert.assertTrue( l_return.get( 1 ).raw() instanceof DoubleMatrix2D );
547 Assert.assertArrayEquals(
548 l_return.get( 1 ).<DoubleMatrix2D>raw().toArray(),
549 new DenseDoubleMatrix2D(
new double[][]{{-0.6618025632357403, -0.7496781758158657}, {-0.7496781758158659, 0.66180256323574}} ).toArray()
552 Assert.assertTrue( l_return.get( 2 ).raw() instanceof DoubleMatrix2D );
553 Assert.assertArrayEquals(
554 l_return.get( 2 ).<DoubleMatrix2D>raw().toArray(),
555 new DenseDoubleMatrix2D(
new double[][]{{-0.8649100931185951, 0.5019268181932333}, {-0.5019268181932333, -0.8649100931185951}} ).toArray()
565 final List<ITerm> l_return =
new ArrayList<>();
569 Stream.of( MATRIX1, MATRIX2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
573 Assert.assertEquals( l_return.size(), 2 );
574 Assert.assertArrayEquals( l_return.stream().map(
ITerm::raw ).toArray(), Stream.of( MATRIX1, MATRIX2 ).toArray() );
584 final List<ITerm> l_return =
new ArrayList<>();
589 new SparseDoubleMatrix2D(
new double[][]{
601 Assert.assertEquals( l_return.size(), 1 );
602 final DoubleMatrix2D l_result = l_return.get( 0 ).raw();
604 IntStream.range( 0, l_result.rows() )
606 .map( l_result::viewRow )
607 .mapToDouble( DoubleMatrix1D::zSum )
608 .forEach( i -> Assert.assertEquals( i, 0, 0 ) );
610 IntStream.range( 0, l_result.columns() )
612 .map( l_result::viewColumn )
613 .mapToDouble( DoubleMatrix1D::zSum )
614 .forEach( i -> Assert.assertEquals( i, 0, 0 ) );
624 final List<ITerm> l_return =
new ArrayList<>();
629 new SparseDoubleMatrix2D(
new double[][]{
641 Assert.assertEquals( l_return.size(), 1 );
642 final DoubleMatrix2D l_result = l_return.get( 0 ).raw();
644 IntStream.range( 0, l_result.rows() ).boxed().forEach( i -> Assert.assertEquals( l_result.getQuick( i, i ), 1, 0 ) );
645 IntStream.range( 0, l_result.rows() )
647 .map( l_result::viewRow )
648 .mapToDouble( DoubleMatrix1D::zSum )
649 .forEach( i -> Assert.assertEquals( i, 0, 1e-10 ) );
658 final List<ITerm> l_return =
new ArrayList<>();
663 new SparseDoubleMatrix2D(
new double[][]{
669 {1, 2, 3, -1, -2, -3}
675 Assert.assertEquals( l_return.size(), 1 );
676 Assert.assertArrayEquals(
677 Arrays.stream( l_return.get( 0 ).<DoubleMatrix1D>raw().toArray() ).boxed().toArray(),
678 Stream.of( 1D, 3D, 6D, 10D, 15D, 0D ).toArray()
689 final List<ITerm> l_return =
new ArrayList<>();
694 new SparseDoubleMatrix2D(
new double[][]{
700 {1, 2, 3, -1, -2, -3}
706 Assert.assertEquals( l_return.size(), 1 );
707 Assert.assertArrayEquals(
708 Arrays.stream( l_return.get( 0 ).<DoubleMatrix1D>raw().toArray() ).boxed().toArray(),
709 Stream.of( 6D, 10D, 12D, 7D, 3D, -3D ).toArray()
720 final int l_size = Math.abs(
new Random().nextInt( 98 ) + 2 );
721 final List<ITerm> l_return =
new ArrayList<>();
725 Stream.of( l_size ).map(
CRawTerm::from ).collect( Collectors.toList() ),
729 Assert.assertEquals( l_return.size(), 1 );
730 final DoubleMatrix2D l_result = l_return.get( 0 ).raw();
733 IntStream.range( 0, l_result.rows() )
735 .flatMap( i -> IntStream.range( 0, l_result.columns() )
737 .map( j -> i.equals( j ) ? l_result.getQuick( i, j ) == 1D : l_result.getQuick( i, j ) == 0D )
750 final List<ITerm> l_return =
new ArrayList<>();
751 final double[] l_data =
new double[]{1, 3, 5, 11};
756 new DenseDoubleMatrix1D( l_data )
761 Assert.assertEquals( l_return.size(), 1 );
762 final DoubleMatrix2D l_result = l_return.get( 0 ).raw();
764 Assert.assertArrayEquals(
765 Arrays.stream( l_data ).boxed().toArray(),
766 IntStream.range( 0, l_result.rows() )
768 .map( i -> l_result.getQuick( i, i ) )
772 IntStream.range( 0, l_result.rows() )
773 .forEach( i -> IntStream.range( 0, l_result.columns() )
774 .filter( j -> i != j )
775 .forEach( j -> Assert.assertEquals( l_result.getQuick( i, j ), 0, 0 ) ) );
final void diagonal()
test diagonal
final void parse()
test parse
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
creates a dense- or sparse-matrix.
returns the frobenius- / matrix-norm of a matrix.
converts a matrix rowise to a list.
final void eigen()
test eigen
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
returns a diagonal matrix.
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
creates a dense- or sparse-matrix from a string.
final void assignscalar()
test assign scalar
returns the column number of a bit matrix.
final void invert()
test invert
returns a single row of a bit matrix.
IContext EMPTYPLAN
empty context with plan
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 void power()
test power
returns a single column of a matrix.
set a single element of a matrix.
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
creates the graph laplacian.
returns the one-norm of a matrix.
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
returns the dimension (rows / columns) of a matrix.
creates the real eigenvalues and eigenvectors of a matrix.
returns the number of non-zero cells.
final void columsum()
test column sum
returns a single element of a matrix.
execution context with local data
returns the two-norm of a matrix.
returns the column-sum of a matrix.
external action interface
adds an element to the list.
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
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
returns the row-sum of a matrix.
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
final void identity()
test identity
returns the row number of a matrix.
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
solver of matrix-equation.
final void singularvalue()
test singularvalue
final void submatrix()
test submatrix
converts a set to a list.
final void copy()
test copy
final void tolist()
test toList
returns the matrix power.
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
returns the condition of a matrix.
returns an element of the list by the index.
returns the determinant of a matrix.
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
returns a single column of a bit matrix.
< T > T raw()
cast to any raw value type
final void assignmatrix()
test assign matrix
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
creates the normalized graph laplacian.
final void solve()
test solve
final void transpose()
test transpose
returns the column number of a matrix.
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
returns a single row of a matrix.
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
test math blas matrix functions
final void column()
test column
creates the trace of the matrix.
static Object [] generator()
data provider generator
final void create()
test create
returns the row number of a matrix.
returns the infinitiy-norm of a matrix.
creates the singular value decomposition of a matrix.
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 void graphlaplacian()
test graph-laplacian
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
assigns a value or matrix to all elements.
final void normalizedgraphlaplacian()
test normalized graph-laplacian
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
returns a copy of the matrix.
returns a copy of the matrix.
final void rowsum()
test row sum
returns the identity matrix.
term structure for simple datatypes
returns the dimension (rows / columns) of a bit matrix.