CStirling.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;

  24. import com.codepoetics.protonpack.StreamUtils;
  25. import org.apache.commons.math3.util.CombinatoricsUtils;
  26. import org.lightjason.agentspeak.action.builtin.IBuiltinAction;
  27. import org.lightjason.agentspeak.language.CCommon;
  28. import org.lightjason.agentspeak.language.CRawTerm;
  29. import org.lightjason.agentspeak.language.ITerm;
  30. import org.lightjason.agentspeak.language.execution.IContext;
  31. import org.lightjason.agentspeak.language.fuzzy.CFuzzyValue;
  32. import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue;

  33. import javax.annotation.Nonnegative;
  34. import javax.annotation.Nonnull;
  35. import java.util.List;


  36. /**
  37.  * action for calculating stirling number.
  38.  * The  action calculates the stirling number
  39.  * with \f$ S(n,k)=\left\{\begin{matrix} n \\ k \end{matrix}\right\}= S_n^{(k)} \f$
  40.  * of each tuple of the unflatten argument list, n is the first value of the tupel
  41.  * and k is the second value of the tupel, the action fails never
  42.  *
  43.  * {@code [S1|S2] = math/stirling(2,3, [4,5]);}
  44.  * @see https://en.wikipedia.org/wiki/Stirling_number
  45.  */
  46. public final class CStirling extends IBuiltinAction
  47. {
  48.     /**
  49.      * serial id
  50.      */
  51.     private static final long serialVersionUID = -9056209645109382946L;

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

  58.     @Nonnull
  59.     @Override
  60.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  61.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
  62.     {
  63.         StreamUtils.windowed(
  64.             CCommon.flatten( p_argument )
  65.                    .map( ITerm::<Number>raw )
  66.                    .mapToInt( Number::intValue )
  67.                    .boxed(),
  68.             2,
  69.             2
  70.         )
  71.                    .map( i -> CombinatoricsUtils.stirlingS2( i.get( 0 ), i.get( 1 ) ) )
  72.                    .map( CRawTerm::from )
  73.                    .forEach( p_return::add );

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

  76. }