CSolve.java

  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. package org.lightjason.agentspeak.action.builtin.math.blas.matrix;

  24. import cern.colt.matrix.tdouble.DoubleMatrix1D;
  25. import cern.colt.matrix.tdouble.DoubleMatrix2D;
  26. import cern.colt.matrix.tdouble.impl.DenseDoubleMatrix2D;
  27. import com.codepoetics.protonpack.StreamUtils;
  28. import org.lightjason.agentspeak.action.builtin.math.blas.IAlgebra;
  29. import org.lightjason.agentspeak.language.CCommon;
  30. import org.lightjason.agentspeak.language.CRawTerm;
  31. import org.lightjason.agentspeak.language.ITerm;
  32. import org.lightjason.agentspeak.language.execution.IContext;
  33. import org.lightjason.agentspeak.language.fuzzy.CFuzzyValue;
  34. import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue;

  35. import javax.annotation.Nonnegative;
  36. import javax.annotation.Nonnull;
  37. import java.util.List;


  38. /**
  39.  * solver of matrix-equation.
  40.  * The action solve the equation \f$ A \cdot X = B \f$
  41.  * for each input tuple, \f$ A \f$ is the first matrix argument
  42.  * within the tuple and \f$ B \f$ the second, which can be a
  43.  * matrix or vector, for each tuple the action returns \f$ X \f$,
  44.  * the action never fails
  45.  *
  46.  * {@code [R1|R2] = math/blas/matrix( Matrix1, Matrix2, [Matrix3, Vector1] );}
  47.  */
  48. public final class CSolve extends IAlgebra
  49. {
  50.     /**
  51.      * serial id
  52.      */
  53.     private static final long serialVersionUID = -2024863045333250337L;

  54.     @Nonnegative
  55.     @Override
  56.     public final int minimalArgumentNumber()
  57.     {
  58.         return 2;
  59.     }

  60.     @Nonnull
  61.     @Override
  62.     @SuppressWarnings( "unchecked" )
  63.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  64.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
  65.     {
  66.         StreamUtils.windowed(
  67.             CCommon.flatten( p_argument ),
  68.             2
  69.         )
  70.             .map( i -> DENSEALGEBRA.solve( i.get( 0 ).<DoubleMatrix2D>raw(), CSolve.result( i.get( 1 ) ) ) )
  71.             .map( CRawTerm::from )
  72.             .forEach( p_return::add );

  73.         return CFuzzyValue.from( true );
  74.     }

  75.     /**
  76.      * creates a matrix from the input term
  77.      *
  78.      * @param p_term term with vector or matrix
  79.      * @return matrix
  80.      */
  81.     @Nonnull
  82.     private static DoubleMatrix2D result( @Nonnull final ITerm p_term )
  83.     {
  84.         if ( CCommon.rawvalueAssignableTo( p_term, DoubleMatrix2D.class ) )
  85.             return p_term.<DoubleMatrix2D>raw();

  86.         final DoubleMatrix2D l_result = new DenseDoubleMatrix2D( Long.valueOf( p_term.<DoubleMatrix1D>raw().size() ).intValue(), 1 );
  87.         l_result.viewColumn( 0 ).assign( p_term.<DoubleMatrix1D>raw() );
  88.         return l_result;
  89.     }
  90. }