LightJason - AgentSpeak(L++)
CElementWise.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.math.blas;
25 
26 import cern.colt.function.tdouble.DoubleDoubleFunction;
27 import cern.colt.matrix.tdouble.DoubleMatrix1D;
28 import cern.colt.matrix.tdouble.DoubleMatrix2D;
29 import cern.jet.math.tdouble.DoubleFunctions;
30 import com.codepoetics.protonpack.StreamUtils;
38 
39 import javax.annotation.Nonnegative;
40 import javax.annotation.Nonnull;
41 import java.util.List;
42 import java.util.function.BiFunction;
43 
44 
54 public final class CElementWise extends IBuiltinAction
55 {
59  private static final long serialVersionUID = -2655464156364927632L;
60 
64  public CElementWise()
65  {
66  super( 3 );
67  }
68 
69  @Nonnegative
70  @Override
71  public final int minimalArgumentNumber()
72  {
73  return 3;
74  }
75 
76  @Nonnull
77  @Override
78  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
79  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
80  {
81  return CFuzzyValue.from(
82  StreamUtils.windowed(
83  CCommon.flatten( p_argument ),
84  3,
85  3
86  ).allMatch( i ->
87  {
88 
89  switch ( i.get( 1 ).<String>raw().trim() )
90  {
91  case "+" :
92  return CElementWise.apply( i.get( 0 ), i.get( 2 ), DoubleFunctions.plus, ( n, m ) -> n + m, p_return );
93 
94  case "|+|" :
95  return CElementWise.apply( i.get( 0 ), i.get( 2 ), DoubleFunctions.plusAbs, ( n, m ) -> Math.abs( n + m ), p_return );
96 
97  case "-" :
98  return CElementWise.apply( i.get( 0 ), i.get( 2 ), DoubleFunctions.minus, ( n, m ) -> n - m, p_return );
99 
100  case "*" :
101  return CElementWise.apply( i.get( 0 ), i.get( 2 ), DoubleFunctions.mult, ( n, m ) -> n * m, p_return );
102 
103  case "/" :
104  return CElementWise.apply( i.get( 0 ), i.get( 2 ), DoubleFunctions.div, ( n, m ) -> n / m, p_return );
105 
106  default:
107  return false;
108  }
109 
110  } )
111  );
112  }
113 
114 
128  private static boolean apply( final ITerm p_left, final ITerm p_right,
129  final DoubleDoubleFunction p_matrixfunction, final BiFunction<Double, Double, Double> p_scalarfunction,
130  final List<ITerm> p_return )
131  {
132  // operation for matrix
133  if ( CCommon.rawvalueAssignableTo( p_left, DoubleMatrix2D.class ) )
134  {
135  final DoubleMatrix2D l_assign = p_left.<DoubleMatrix2D>raw().copy();
136 
137  if ( CCommon.rawvalueAssignableTo( p_right, DoubleMatrix2D.class ) )
138  {
139  l_assign.assign( p_right.<DoubleMatrix2D>raw(), p_matrixfunction );
140  p_return.add( CRawTerm.from( l_assign ) );
141  return true;
142  }
143 
144  if ( CCommon.rawvalueAssignableTo( p_right, Number.class ) )
145  {
146  l_assign.assign( i -> p_scalarfunction.apply( i, p_right.<Number>raw().doubleValue() ) );
147  p_return.add( CRawTerm.from( l_assign ) );
148  return true;
149  }
150  }
151 
152  // operation for vector
153  if ( CCommon.rawvalueAssignableTo( p_left, DoubleMatrix1D.class ) )
154  {
155  final DoubleMatrix1D l_assign = p_left.<DoubleMatrix1D>raw().copy();
156 
157  if ( CCommon.rawvalueAssignableTo( p_right, DoubleMatrix1D.class ) )
158  {
159  l_assign.assign( p_right.<DoubleMatrix1D>raw(), p_matrixfunction );
160  p_return.add( CRawTerm.from( l_assign ) );
161  return true;
162  }
163 
164  if ( CCommon.rawvalueAssignableTo( p_right, Number.class ) )
165  {
166  l_assign.assign( i -> p_scalarfunction.apply( i, p_right.<Number>raw().doubleValue() ) );
167  p_return.add( CRawTerm.from( l_assign ) );
168  return true;
169  }
170  }
171 
172  return false;
173  }
174 
175 
176 
177 }
base class of build-in actions for setting name by package/classname (without prefix character) ...
static boolean apply(final ITerm p_left, final ITerm p_right, final DoubleDoubleFunction p_matrixfunction, final BiFunction< Double, Double, Double > p_scalarfunction, final List< ITerm > p_return)
elementwise assign
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
final int minimalArgumentNumber()
minimum number of arguments
execution context with local data
Definition: IContext.java:42
static Stream< ITerm > flatten( @Nonnull final Collection<? extends ITerm > p_terms)
flat term-in-term collection into a straight term 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
static< T > boolean rawvalueAssignableTo( @Nonnull final T p_value, @Nonnull final Class<?>... p_class)
checks a term value for assignable class
result for an immutable fuzzy value
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