CColumnSum.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.DenseDoubleMatrix1D;
  27. import cern.colt.matrix.tdouble.impl.SparseDoubleMatrix1D;
  28. import org.lightjason.agentspeak.action.builtin.math.blas.EType;
  29. import org.lightjason.agentspeak.action.builtin.math.blas.IAlgebra;
  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.IntStream;


  40. /**
  41.  * returns the column-sum of a matrix.
  42.  * The action returns the column-sum of all matrix objects,
  43.  * a string value defines a sparse or dense resulting vector,
  44.  * the action never fails
  45.  *
  46.  * {@code
  47.     [S1|S2] = math/blas/matrix/columnsum( Matrix1, Matrix2 );
  48.     [S1|S2] = math/blas/matrix/columnsum( Matrix1, Matrix2, "sparse" );
  49.  * }
  50.  */
  51. public final class CColumnSum extends IAlgebra
  52. {
  53.     /**
  54.      * serial id
  55.      */
  56.     private static final long serialVersionUID = -6504165889102496768L;

  57.     @Nonnegative
  58.     @Override
  59.     public final int minimalArgumentNumber()
  60.     {
  61.         return 1;
  62.     }

  63.     @Nonnull
  64.     @Override
  65.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  66.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
  67.     {
  68.         final EType l_type = CCommon.flatten( p_argument )
  69.                                     .parallel()
  70.                                     .filter( i -> CCommon.rawvalueAssignableTo( i, String.class ) )
  71.                                     .findFirst()
  72.                                     .map( ITerm::<String>raw )
  73.                                     .map( EType::from )
  74.                                     .orElse( EType.DENSE );

  75.         CCommon.flatten( p_argument )
  76.                .filter( i -> CCommon.rawvalueAssignableTo( i, DoubleMatrix2D.class ) )
  77.                .map( ITerm::<DoubleMatrix2D>raw )
  78.                .map( i -> IntStream.range( 0, i.columns() ).boxed().map( i::viewColumn ).mapToDouble( DoubleMatrix1D::zSum ).toArray() )
  79.                .map( i -> generate( i, l_type ) )
  80.                .map( CRawTerm::from )
  81.                .forEach( p_return::add );

  82.         return CFuzzyValue.from( true );
  83.     }

  84.     /**
  85.      * generates a vector
  86.      *
  87.      * @param p_value values
  88.      * @param p_type type
  89.      * @return vector
  90.      */
  91.     @Nonnull
  92.     private static DoubleMatrix1D generate( @Nonnull final double[] p_value, @Nonnull final EType p_type )
  93.     {
  94.         switch ( p_type )
  95.         {
  96.             case SPARSE: return new SparseDoubleMatrix1D( p_value );
  97.             default : return new DenseDoubleMatrix1D( p_value );
  98.         }
  99.     }

  100. }