aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
tensor.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
47#ifndef GUM_TENSOR_H
48#define GUM_TENSOR_H
49
50#include <functional>
51#include <vector>
52
53#include <agrum/agrum.h>
54
56
57namespace gum {
58 // ==========================================================================
59 // === TENSOR ===
60 // ==========================================================================
61
84 template < typename GUM_SCALAR >
85 class Tensor final: public MultiDimDecorator< GUM_SCALAR > {
86 public:
87 static Tensor< GUM_SCALAR > deterministicTensor(const DiscreteVariable& var, Idx value);
88 static Tensor< GUM_SCALAR > deterministicTensor(const DiscreteVariable& var,
89 const std::string& label);
90 static Tensor< GUM_SCALAR > uniformTensor(const DiscreteVariable& var);
91 // =========================================================================
93 // =========================================================================
95
102 Tensor();
103
110 Tensor(const std::vector< const DiscreteVariable* >& vars);
111
117
133 Tensor(const Tensor< GUM_SCALAR >& src);
134 Tensor< GUM_SCALAR >& operator=(const Tensor< GUM_SCALAR >& src);
135
139 Tensor(Tensor< GUM_SCALAR >&& from);
140 Tensor< GUM_SCALAR >& operator=(Tensor< GUM_SCALAR >&& src);
141
142
146 ~Tensor() final;
147
149 // ========================================================================
151 // ========================================================================
153
154 Tensor< GUM_SCALAR >* newFactory() const final;
155
157 // ========================================================================
159 // ========================================================================
161
162 const Tensor< GUM_SCALAR >& random() const;
163
165 const Tensor< GUM_SCALAR >& randomDistribution() const;
166
168 const Tensor< GUM_SCALAR >& randomCPT() const;
169
173 const Tensor< GUM_SCALAR >& noising(GUM_SCALAR alpha) const;
174
180 Tensor< GUM_SCALAR > sumOut(const gum::VariableSet& del_vars) const;
181
187 Tensor< GUM_SCALAR > sumIn(const gum::VariableSet& kept_vars) const;
188
194 Tensor< GUM_SCALAR > prodOut(const gum::VariableSet& del_vars) const;
195
201 Tensor< GUM_SCALAR > prodIn(const gum::VariableSet& kept_vars) const;
202
208 Tensor< GUM_SCALAR > minOut(const gum::VariableSet& del_vars) const;
209
215 Tensor< GUM_SCALAR > minIn(const gum::VariableSet& kept_vars) const;
216
222 Tensor< GUM_SCALAR > maxOut(const gum::VariableSet& del_vars) const;
223
229 Tensor< GUM_SCALAR > maxIn(const gum::VariableSet& kept_vars) const;
230
234 Tensor< GUM_SCALAR > isNonZeroMap() const;
235
237 GUM_SCALAR sum() const;
239 GUM_SCALAR product() const;
241 GUM_SCALAR max() const;
243 GUM_SCALAR min() const;
246 GUM_SCALAR maxNonOne() const;
249 GUM_SCALAR minNonZero() const;
250
252 Set< Instantiation > findAll(GUM_SCALAR v) const;
253
255 std::pair< Set< Instantiation >, GUM_SCALAR > argmax() const;
256
258 std::pair< Set< Instantiation >, GUM_SCALAR > argmin() const;
259
266 GUM_SCALAR expectedValue(std::function< GUM_SCALAR(const gum::Instantiation&) >) const;
267
269 GUM_SCALAR entropy() const;
270
275 Tensor< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable* >& vars) const;
276
281 Tensor< GUM_SCALAR > reorganize(const std::vector< std::string >& vars) const;
282
286 Tensor< GUM_SCALAR > extract(const Instantiation& inst) const;
287
291 Tensor< GUM_SCALAR > putFirst(const DiscreteVariable* var) const;
292
296 Tensor< GUM_SCALAR > putFirst(const std::string& varname) const;
297
306 const Tensor< GUM_SCALAR >& fillWith(const Tensor< GUM_SCALAR >& src) const;
307
324 const Tensor< GUM_SCALAR >& fillWith(const Tensor< GUM_SCALAR >& src,
325 const std::vector< std::string >& mapSrc) const;
326
335 const Tensor< GUM_SCALAR >& fillWith(const std::vector< GUM_SCALAR >& data) const;
336
342 const Tensor< GUM_SCALAR >& fillWith(const GUM_SCALAR& val) const;
343
344
348 const Tensor< GUM_SCALAR >& abs() const;
349
353 const Tensor< GUM_SCALAR >& sq() const;
354
358 const Tensor< GUM_SCALAR >& log2() const;
359
365 const Tensor< GUM_SCALAR >& sgn() const;
366
371 Tensor< GUM_SCALAR > new_abs() const;
372
377 Tensor< GUM_SCALAR > new_sq() const;
378
383 Tensor< GUM_SCALAR > new_log2() const;
384
390 Tensor< GUM_SCALAR > new_sgn() const;
391
392
396 const Tensor< GUM_SCALAR >& normalize() const;
397
406 GUM_SCALAR KL(const Tensor< GUM_SCALAR >& p) const;
407
415 const Tensor< GUM_SCALAR >& normalizeAsCPT(const Idx& varId = 0) const;
416
420 const Tensor< GUM_SCALAR >& scale(GUM_SCALAR v) const;
421
425 const Tensor< GUM_SCALAR >& translate(GUM_SCALAR v) const;
426
430 const Tensor< GUM_SCALAR >& inverse() const;
431
435 [[nodiscard]] Idx draw() const;
436
441 Size memoryFootprint() const;
442
444
445 // ========================================================================
447 // ========================================================================
449 Tensor< GUM_SCALAR > operator+(const Tensor< GUM_SCALAR >& p2) const;
450 Tensor< GUM_SCALAR > operator+(const GUM_SCALAR& v) const;
451 Tensor< GUM_SCALAR > operator-(const Tensor< GUM_SCALAR >& p2) const;
452 Tensor< GUM_SCALAR > operator-(const GUM_SCALAR& v) const;
453 Tensor< GUM_SCALAR > operator*(const Tensor< GUM_SCALAR >& p2) const;
454 Tensor< GUM_SCALAR > operator*(const GUM_SCALAR& v) const;
455 Tensor< GUM_SCALAR > operator/(const Tensor< GUM_SCALAR >& p2) const;
456 Tensor< GUM_SCALAR > operator/(const GUM_SCALAR& v) const;
457 Tensor< GUM_SCALAR >& operator+=(const Tensor< GUM_SCALAR >& r);
458 Tensor< GUM_SCALAR >& operator+=(const GUM_SCALAR& v);
459 Tensor< GUM_SCALAR >& operator*=(const Tensor< GUM_SCALAR >& r);
460 Tensor< GUM_SCALAR >& operator*=(const GUM_SCALAR& v);
461 Tensor< GUM_SCALAR >& operator-=(const Tensor< GUM_SCALAR >& r);
462 Tensor< GUM_SCALAR >& operator-=(const GUM_SCALAR& v);
463 Tensor< GUM_SCALAR >& operator/=(const Tensor< GUM_SCALAR >& r);
464 Tensor< GUM_SCALAR >& operator/=(const GUM_SCALAR& v);
465
466 // these operations are only defined for boolean-like Tensor (evidence/likelihood)
467 [[nodiscard]] bool isEvidence() const;
468 Tensor< GUM_SCALAR > operator|(const Tensor< GUM_SCALAR >& p2) const;
469 Tensor< GUM_SCALAR > operator&(const Tensor< GUM_SCALAR >& p2) const;
470 Tensor< GUM_SCALAR > operator~() const;
471
472 bool operator==(const Tensor< GUM_SCALAR >& r) const;
473
474 Tensor< GUM_SCALAR >& operator<<(const DiscreteVariable& v);
475
476 [[nodiscard]] std::string toString() const final;
477
481 static Tensor< GUM_SCALAR > evEq(const DiscreteVariable& v, double val);
482 static Tensor< GUM_SCALAR > evIn(const DiscreteVariable& v, double val1, double val2);
483 static Tensor< GUM_SCALAR > evGt(const DiscreteVariable& v, double val);
484 static Tensor< GUM_SCALAR > evLt(const DiscreteVariable& v, double val);
486
487 protected:
488 [[nodiscard]] gum::VariableSet complementVars_(const gum::VariableSet& del_vars) const;
489 };
490
491#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
492 extern template class Tensor< double >;
493#endif
494
495 template < typename GUM_SCALAR >
496 inline Tensor< GUM_SCALAR > log2(const Tensor< GUM_SCALAR >& arg) {
497 return arg.new_log2();
498 }
499
500 template < typename GUM_SCALAR >
501 inline Tensor< GUM_SCALAR > abs(const Tensor< GUM_SCALAR >& arg) {
502 return arg.new_abs();
503 }
504
505 template < typename GUM_SCALAR >
506 inline Tensor< GUM_SCALAR > sq(const Tensor< GUM_SCALAR >& arg) {
507 return arg.new_sq();
508 }
509} /* namespace gum */
510
512#endif /* GUM_TENSOR_H */
Base class for discrete random variable.
Class for assigning/browsing values to tuples of discrete variables.
Abstract base class for all multi dimensionnal containers.
MultiDimDecorator(MultiDimImplementation< GUM_SCALAR > *aContent=nullptr, GUM_SCALAR empty_value=(GUM_SCALAR) 0)
Class constructor.
<agrum/base/multidim/multiDimImplementation.h>
Representation of a set.
Definition set.h:131
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Definition tensor.h:85
Tensor< GUM_SCALAR > & operator-=(const Tensor< GUM_SCALAR > &r)
the function to be used to add two Tensors
Definition tensor_tpl.h:964
Tensor< GUM_SCALAR > & operator/=(const Tensor< GUM_SCALAR > &r)
the function to be used to add two Tensors
Definition tensor_tpl.h:976
const Tensor< GUM_SCALAR > & fillWith(const Tensor< GUM_SCALAR > &src) const
copy a Tensor data using name of variables and labels (not necessarily the same variables in the same...
Definition tensor_tpl.h:271
const Tensor< GUM_SCALAR > & normalizeAsCPT(const Idx &varId=0) const
normalisation of this as a CPT for the variable varId
Definition tensor_tpl.h:405
Idx draw() const
get a value at random from a 1-D distribution
Definition tensor_tpl.h:696
Tensor< GUM_SCALAR > operator|(const Tensor< GUM_SCALAR > &p2) const
the function to be used to add two Tensors
Definition tensor_tpl.h:847
GUM_SCALAR minNonZero() const
min of all non zero elements in the Tensor
Definition tensor_tpl.h:215
std::pair< Set< Instantiation >, GUM_SCALAR > argmin() const
Pair of the set of instantiation corresponding to the min and this min in the Tensor.
Definition tensor_tpl.h:739
Tensor< GUM_SCALAR > extract(const Instantiation &inst) const
create a new Tensor extracted from *this given a partial instantiation
Definition tensor_tpl.h:688
const Tensor< GUM_SCALAR > & random() const
generate a random Tensor with each parameter in [0,1]
Definition tensor_tpl.h:745
Tensor< GUM_SCALAR > new_sq() const
Create a new tensor and apply $x^2$ on every element of the container.
Definition tensor_tpl.h:802
Tensor< GUM_SCALAR > putFirst(const DiscreteVariable *var) const
create a new Tensor with a certain variable in first
Definition tensor_tpl.h:658
Tensor< GUM_SCALAR > prodIn(const gum::VariableSet &kept_vars) const
Projection using multiplication as operation (and implementation-optimized operations).
Definition tensor_tpl.h:558
Tensor< GUM_SCALAR > maxOut(const gum::VariableSet &del_vars) const
Projection using max as operation (and implementation-optimized operations).
Definition tensor_tpl.h:522
gum::VariableSet complementVars_(const gum::VariableSet &del_vars) const
Definition tensor_tpl.h:607
Tensor< GUM_SCALAR > * newFactory() const final
Default implementation of MultiDimContainer::set().
Definition tensor_tpl.h:150
static Tensor< GUM_SCALAR > evEq(const DiscreteVariable &v, double val)
numerical evidence generator
Tensor< GUM_SCALAR > sumOut(const gum::VariableSet &del_vars) const
Projection using sum as operation (and implementation-optimized operations).
Definition tensor_tpl.h:455
Tensor< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable * > &vars) const
create a new Tensor with another order
Definition tensor_tpl.h:618
Tensor< GUM_SCALAR > operator&(const Tensor< GUM_SCALAR > &p2) const
the function to be used to add two Tensors
Definition tensor_tpl.h:862
Size memoryFootprint() const
compute the (approximated) footprint in memory of the tensor
Tensor< GUM_SCALAR > new_sgn() const
Create a new tensor and apply sgn(x)$ on every element of the container.
Definition tensor_tpl.h:812
GUM_SCALAR maxNonOne() const
max of all non one elements in the Tensor
Definition tensor_tpl.h:194
bool operator==(const Tensor< GUM_SCALAR > &r) const
the function to be used to add two Tensors
Definition tensor_tpl.h:988
static Tensor< GUM_SCALAR > deterministicTensor(const DiscreteVariable &var, Idx value)
Tensor< GUM_SCALAR > new_log2() const
Create a new tensor and apply $log_2(x)$ on every element of the container.
Definition tensor_tpl.h:807
static Tensor< GUM_SCALAR > uniformTensor(const DiscreteVariable &var)
const Tensor< GUM_SCALAR > & scale(GUM_SCALAR v) const
multiply (each value of) *this by v
Definition tensor_tpl.h:437
GUM_SCALAR entropy() const
entropy of the Tensor
Definition tensor_tpl.h:250
Tensor< GUM_SCALAR > minOut(const gum::VariableSet &del_vars) const
Projection using min as operation (and implementation-optimized operations).
Definition tensor_tpl.h:500
GUM_SCALAR KL(const Tensor< GUM_SCALAR > &p) const
compute KL divergence between this and p Checks the compatibility and then compute KL divergence
Definition tensor_tpl.h:349
static Tensor< GUM_SCALAR > evGt(const DiscreteVariable &v, double val)
numerical evidence generator
~Tensor() final
Destructor.
Definition tensor_tpl.h:144
Tensor< GUM_SCALAR > sumIn(const gum::VariableSet &kept_vars) const
Projection using sum as operation (and implementation-optimized operations).
Definition tensor_tpl.h:544
Set< Instantiation > findAll(GUM_SCALAR v) const
set of instantiation corresponding to the parameter v in the Tensor
Definition tensor_tpl.h:718
static Tensor< GUM_SCALAR > evIn(const DiscreteVariable &v, double val1, double val2)
numerical evidence generator
const Tensor< GUM_SCALAR > & sq() const
apply $x^2$ on every element of the container
Definition tensor_tpl.h:331
const Tensor< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
Definition tensor_tpl.h:390
std::pair< Set< Instantiation >, GUM_SCALAR > argmax() const
Pair of the set of instantiation corresponding to the max and this max in the Tensor.
Definition tensor_tpl.h:732
const Tensor< GUM_SCALAR > & translate(GUM_SCALAR v) const
add v to (each value of) *this
Definition tensor_tpl.h:443
const Tensor< GUM_SCALAR > & noising(GUM_SCALAR alpha) const
add a noise in a CPT by mixing (1-alpha)this+alpha.randomCPT()
Definition tensor_tpl.h:788
const Tensor< GUM_SCALAR > & sgn() const
apply sgn(x)$ on every element of the container
Definition tensor_tpl.h:343
bool isEvidence() const
is an evidence ? (marginal-like but has not to sum to 1)
Definition tensor_tpl.h:839
Tensor< GUM_SCALAR > & operator=(const Tensor< GUM_SCALAR > &src)
Default constructor.
Definition tensor_tpl.h:124
const Tensor< GUM_SCALAR > & randomDistribution() const
generate a random Distribution in the Tensor
Definition tensor_tpl.h:759
GUM_SCALAR sum() const
sum of all elements in the Tensor
Definition tensor_tpl.h:157
const Tensor< GUM_SCALAR > & randomCPT() const
generate a random CPT in the Tensor
Definition tensor_tpl.h:770
Tensor< GUM_SCALAR > operator/(const Tensor< GUM_SCALAR > &p2) const
the function to be used to divide two Tensors
Definition tensor_tpl.h:923
Tensor< GUM_SCALAR > minIn(const gum::VariableSet &kept_vars) const
Projection using min as operation (and implementation-optimized operations).
Definition tensor_tpl.h:571
Tensor()
Default constructor.
Definition tensor_tpl.h:61
GUM_SCALAR max() const
max of all elements in the Tensor
Definition tensor_tpl.h:175
GUM_SCALAR expectedValue(std::function< GUM_SCALAR(const gum::Instantiation &) >) const
ExpectedValue computes the expectation of f over *this.
Definition tensor_tpl.h:233
Tensor< GUM_SCALAR > new_abs() const
Create a new tensor and apply abs on every element of the container.
Definition tensor_tpl.h:797
Tensor< GUM_SCALAR > maxIn(const gum::VariableSet &kept_vars) const
Projection using max as operation (and implementation-optimized operations).
Definition tensor_tpl.h:584
GUM_SCALAR product() const
product of all elements in the Tensor
Definition tensor_tpl.h:166
Tensor< GUM_SCALAR > & operator<<(const DiscreteVariable &v)
the function to be used to add two Tensors
Definition tensor_tpl.h:817
const Tensor< GUM_SCALAR > & abs() const
Apply abs on every element of the container.
Definition tensor_tpl.h:379
const Tensor< GUM_SCALAR > & log2() const
apply $log_2(x)$ on every element of the container
Definition tensor_tpl.h:337
Tensor< GUM_SCALAR > prodOut(const gum::VariableSet &del_vars) const
Projection using multiplication as operation (and implementation-optimized operations).
Definition tensor_tpl.h:478
Tensor< GUM_SCALAR > operator-(const Tensor< GUM_SCALAR > &p2) const
the function to be used to subtract two Tensors
Definition tensor_tpl.h:890
Tensor< GUM_SCALAR > & operator*=(const Tensor< GUM_SCALAR > &r)
the function to be used to add two Tensors
Definition tensor_tpl.h:952
Tensor< GUM_SCALAR > isNonZeroMap() const
create a boolean-like tensor using the predicate isNonZero
Definition tensor_tpl.h:597
std::string toString() const final
the function to be used to add two Tensors
Definition tensor_tpl.h:999
GUM_SCALAR min() const
min of all elements in the Tensor
Definition tensor_tpl.h:184
static Tensor< GUM_SCALAR > evLt(const DiscreteVariable &v, double val)
numerical evidence generator
Tensor< GUM_SCALAR > & operator+=(const Tensor< GUM_SCALAR > &r)
the function to be used to add two Tensors
Definition tensor_tpl.h:940
Tensor< GUM_SCALAR > operator+(const Tensor< GUM_SCALAR > &p2) const
the function to be used to add two Tensors
Definition tensor_tpl.h:824
const Tensor< GUM_SCALAR > & inverse() const
the function to inverse (each value of) *this
Definition tensor_tpl.h:449
Tensor< GUM_SCALAR > operator~() const
the function to be used to add two Tensors
Definition tensor_tpl.h:877
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
Headers for MultiDimDecorator.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
value_type & operator*()
Returns the value pointed to by the iterator.
Set< const DiscreteVariable * > VariableSet
Tensor< GUM_SCALAR > log2(const Tensor< GUM_SCALAR > &arg)
Definition tensor.h:496
Tensor< GUM_SCALAR > sq(const Tensor< GUM_SCALAR > &arg)
Definition tensor.h:506
Tensor< GUM_SCALAR > abs(const Tensor< GUM_SCALAR > &arg)
Definition tensor.h:501
Implementation of the Tensor class.