LightJason - AgentSpeak(L++)
CDiagonal.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.matrix;
25 
26 import cern.colt.matrix.tdouble.DoubleFactory2D;
27 import cern.colt.matrix.tdouble.DoubleMatrix1D;
28 import cern.colt.matrix.tdouble.DoubleMatrix2D;
37 
38 import javax.annotation.Nonnegative;
39 import javax.annotation.Nonnull;
40 import java.util.List;
41 
42 
56 public final class CDiagonal extends IAlgebra
57 {
61  private static final long serialVersionUID = -3604474410543673614L;
62 
63  @Nonnegative
64  @Override
65  public final int minimalArgumentNumber()
66  {
67  return 1;
68  }
69 
70  @Nonnull
71  @Override
72  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
73  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
74  {
75  final EType l_type = CCommon.flatten( p_argument )
76  .parallel()
77  .filter( i -> CCommon.rawvalueAssignableTo( i, String.class ) )
78  .findFirst()
79  .map( ITerm::<String>raw )
80  .map( EType::from )
81  .orElse( EType.SPARSE );
82 
83  CCommon.flatten( p_argument )
84  .filter( i -> CCommon.rawvalueAssignableTo( i, DoubleMatrix1D.class ) )
85  .map( ITerm::<DoubleMatrix1D>raw )
86  .map( i -> generate( i, l_type ) )
87  .map( CRawTerm::from )
88  .forEach( p_return::add );
89 
90 
91  return CFuzzyValue.from( true );
92  }
93 
101  @Nonnull
102  private static DoubleMatrix2D generate( @Nonnull final DoubleMatrix1D p_elements, @Nonnull final EType p_type )
103  {
104  switch ( p_type )
105  {
106  case DENSE: return DoubleFactory2D.dense.diagonal( p_elements );
107  default: return DoubleFactory2D.sparse.diagonal( p_elements );
108  }
109  }
110 }
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
static DoubleMatrix2D generate( @Nonnull final DoubleMatrix1D p_elements, @Nonnull final EType p_type)
generates the diagonal matrix
Definition: CDiagonal.java:102
execution context with local data
Definition: IContext.java:42
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
Definition: CDiagonal.java:72
static Stream< ITerm > flatten( @Nonnull final Collection<? extends ITerm > p_terms)
flat term-in-term collection into a straight term list
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 EType from(final String p_name)
additional factory
Definition: EType.java:53
final int minimalArgumentNumber()
minimum number of arguments
Definition: CDiagonal.java:65
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