CToBlas.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.bit.matrix;

  24. import cern.colt.matrix.tbit.BitMatrix;
  25. import cern.colt.matrix.tdouble.DoubleMatrix2D;
  26. import cern.colt.matrix.tdouble.impl.DenseDoubleMatrix2D;
  27. import cern.colt.matrix.tdouble.impl.SparseDoubleMatrix2D;
  28. import org.lightjason.agentspeak.action.builtin.IBuiltinAction;
  29. import org.lightjason.agentspeak.action.builtin.math.blas.EType;
  30. import org.lightjason.agentspeak.language.CCommon;
  31. import org.lightjason.agentspeak.language.CRawTerm;
  32. import org.lightjason.agentspeak.language.ITerm;
  33. import org.lightjason.agentspeak.language.execution.IContext;
  34. import org.lightjason.agentspeak.language.fuzzy.CFuzzyValue;
  35. import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue;

  36. import javax.annotation.Nonnegative;
  37. import javax.annotation.Nonnull;
  38. import java.util.List;
  39. import java.util.stream.Collectors;
  40. import java.util.stream.IntStream;


  41. /**
  42.  * converts the bit matrix to a blas matrix.
  43.  * The action converts the bit matrix to a blas matrix,
  44.  * the last argument can be "dense" or "sparse", all
  45.  * other arguments are bit matrices and the actions
  46.  * never fails
  47.  *
  48.  * {@code [A|B] = math/bit/matrix/toblas( BitMatrix1, BitMatrix2, "dense | sparse" );}
  49.  */
  50. public final class CToBlas extends IBuiltinAction
  51. {
  52.     /**
  53.      * serial id
  54.      */
  55.     private static final long serialVersionUID = -5682613760524318512L;

  56.     /**
  57.      * ctor
  58.      */
  59.     public CToBlas()
  60.     {
  61.         super( 4 );
  62.     }

  63.     @Nonnegative
  64.     @Override
  65.     public final int minimalArgumentNumber()
  66.     {
  67.         return 1;
  68.     }

  69.     @Nonnull
  70.     @Override
  71.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  72.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
  73.     {
  74.         final List<ITerm> l_arguments = CCommon.flatten( p_argument ).collect( Collectors.toList() );
  75.         final EType l_type = l_arguments.parallelStream()
  76.                                         .filter( i -> CCommon.rawvalueAssignableTo( i, String.class ) )
  77.                                         .findFirst().map( i -> EType.from( i.<String>raw() ) )
  78.                                         .orElse( EType.SPARSE );

  79.         switch ( l_type )
  80.         {
  81.             case DENSE:
  82.                 l_arguments.stream()
  83.                            .filter( i -> CCommon.rawvalueAssignableTo( i, BitMatrix.class ) )
  84.                            .map( ITerm::<BitMatrix>raw )
  85.                            .map( i -> CToBlas.tomatrix( i, new DenseDoubleMatrix2D( i.rows(), i.columns() ) ) )
  86.                            .map( CRawTerm::from )
  87.                            .forEach( p_return::add );

  88.                 return CFuzzyValue.from( true );


  89.             case SPARSE:
  90.                 l_arguments.stream()
  91.                            .filter( i -> CCommon.rawvalueAssignableTo( i, BitMatrix.class ) )
  92.                            .map( ITerm::<BitMatrix>raw )
  93.                            .map( i -> CToBlas.tomatrix( i, new SparseDoubleMatrix2D( i.rows(), i.columns() ) ) )
  94.                            .map( CRawTerm::from )
  95.                            .forEach( p_return::add );

  96.                 return CFuzzyValue.from( true );

  97.             default:

  98.                 return CFuzzyValue.from( false );
  99.         }
  100.     }


  101.     /**
  102.      * converts the bit matrix to the blas matrix
  103.      *
  104.      * @param p_source bit matrix
  105.      * @param p_target blas matrix
  106.      * @return blas matrix
  107.      */
  108.     @Nonnull
  109.     private static DoubleMatrix2D tomatrix( @Nonnull final BitMatrix p_source, @Nonnull final DoubleMatrix2D p_target )
  110.     {
  111.         IntStream.range( 0, p_source.rows() )
  112.                  .forEach( r -> IntStream.range( 0, p_source.columns() ).forEach( c -> p_target.setQuick( r, c, p_source.getQuick( r, c ) ? 1 : 0 ) ) );

  113.         return p_target;
  114.     }
  115. }