aGrUM 3.1.1
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-2026 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-2026 *
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 < GUM_Numeric 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 std::string_view 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 [[nodiscard]] 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
277 GUM_SCALAR mean() const;
278
285 GUM_SCALAR variance() const;
286
294 GUM_SCALAR stdDev() const;
295
300 Tensor< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable* >& vars) const;
301
306 Tensor< GUM_SCALAR > reorganize(const std::vector< std::string >& vars) const;
307
311 Tensor< GUM_SCALAR > extract(const Instantiation& inst) const;
312
316 Tensor< GUM_SCALAR > putFirst(const DiscreteVariable* var) const;
317
321 Tensor< GUM_SCALAR > putFirst(std::string_view varname) const;
322
331 const Tensor< GUM_SCALAR >& fillWith(const Tensor< GUM_SCALAR >& src) const;
332
349 const Tensor< GUM_SCALAR >& fillWith(const Tensor< GUM_SCALAR >& src,
350 const std::vector< std::string >& mapSrc) const;
351
360 const Tensor< GUM_SCALAR >& fillWith(const std::vector< GUM_SCALAR >& data) const;
361
367 const Tensor< GUM_SCALAR >& fillWith(const GUM_SCALAR& val) const;
368
369
373 const Tensor< GUM_SCALAR >& abs() const;
374
378 const Tensor< GUM_SCALAR >& sq() const;
379
383 const Tensor< GUM_SCALAR >& log2() const;
384
390 const Tensor< GUM_SCALAR >& sgn() const;
391
396 Tensor< GUM_SCALAR > new_abs() const;
397
402 Tensor< GUM_SCALAR > new_sq() const;
403
408 Tensor< GUM_SCALAR > new_log2() const;
409
415 Tensor< GUM_SCALAR > new_sgn() const;
416
417
421 const Tensor< GUM_SCALAR >& normalize() const;
422
431 GUM_SCALAR KL(const Tensor< GUM_SCALAR >& p) const;
432
440 const Tensor< GUM_SCALAR >& normalizeAsCPT(const Idx& varId = 0) const;
441
445 const Tensor< GUM_SCALAR >& scale(GUM_SCALAR v) const;
446
450 const Tensor< GUM_SCALAR >& translate(GUM_SCALAR v) const;
451
455 const Tensor< GUM_SCALAR >& inverse() const;
456
460 [[nodiscard]] Idx draw() const;
461
466 Size memoryFootprint() const;
467
469
470 // ========================================================================
472 // ========================================================================
474 Tensor< GUM_SCALAR > operator+(const Tensor< GUM_SCALAR >& p2) const;
475 Tensor< GUM_SCALAR > operator+(const GUM_SCALAR& v) const;
476 Tensor< GUM_SCALAR > operator-(const Tensor< GUM_SCALAR >& p2) const;
477 Tensor< GUM_SCALAR > operator-(const GUM_SCALAR& v) const;
478 Tensor< GUM_SCALAR > operator*(const Tensor< GUM_SCALAR >& p2) const;
479 Tensor< GUM_SCALAR > operator*(const GUM_SCALAR& v) const;
480 Tensor< GUM_SCALAR > operator/(const Tensor< GUM_SCALAR >& p2) const;
481 Tensor< GUM_SCALAR > operator/(const GUM_SCALAR& v) const;
482 Tensor< GUM_SCALAR >& operator+=(const Tensor< GUM_SCALAR >& r);
483 Tensor< GUM_SCALAR >& operator+=(const GUM_SCALAR& v);
484 Tensor< GUM_SCALAR >& operator*=(const Tensor< GUM_SCALAR >& r);
485 Tensor< GUM_SCALAR >& operator*=(const GUM_SCALAR& v);
486 Tensor< GUM_SCALAR >& operator-=(const Tensor< GUM_SCALAR >& r);
487 Tensor< GUM_SCALAR >& operator-=(const GUM_SCALAR& v);
488 Tensor< GUM_SCALAR >& operator/=(const Tensor< GUM_SCALAR >& r);
489 Tensor< GUM_SCALAR >& operator/=(const GUM_SCALAR& v);
490
491 // these operations are only defined for boolean-like Tensor (evidence/likelihood)
492 [[nodiscard]] bool isEvidence() const;
493 Tensor< GUM_SCALAR > operator|(const Tensor< GUM_SCALAR >& p2) const;
494 Tensor< GUM_SCALAR > operator&(const Tensor< GUM_SCALAR >& p2) const;
495 Tensor< GUM_SCALAR > operator~() const;
496
497 bool operator==(const Tensor< GUM_SCALAR >& r) const;
498
499 Tensor< GUM_SCALAR >& operator<<(const DiscreteVariable& v);
500
501 [[nodiscard]] std::string toString() const final;
502
506 static Tensor< GUM_SCALAR > evEq(const DiscreteVariable& v, double val);
507 static Tensor< GUM_SCALAR > evIn(const DiscreteVariable& v, double val1, double val2);
508 static Tensor< GUM_SCALAR > evGt(const DiscreteVariable& v, double val);
509 static Tensor< GUM_SCALAR > evLt(const DiscreteVariable& v, double val);
511
512 private:
513 [[nodiscard]] gum::VariableSet _complementVars_(const gum::VariableSet& del_vars) const;
514 };
515
516#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
517 extern template class Tensor< double >;
518#endif
519
520 template < GUM_Numeric GUM_SCALAR >
521 inline Tensor< GUM_SCALAR > log2(const Tensor< GUM_SCALAR >& arg);
522
523 template < GUM_Numeric GUM_SCALAR >
524 inline Tensor< GUM_SCALAR > abs(const Tensor< GUM_SCALAR >& arg);
525
526 template < GUM_Numeric GUM_SCALAR >
527 inline Tensor< GUM_SCALAR > sq(const Tensor< GUM_SCALAR >& arg);
528} /* namespace gum */
529
531#endif /* GUM_TENSOR_H */
Base class for discrete random variable.
Class for assigning/browsing values to tuples of discrete variables.
MultiDimDecorator(MultiDimImplementation< GUM_SCALAR > *aContent=nullptr, GUM_SCALAR empty_value=(GUM_SCALAR) 0)
Representation of a set.
Definition set.h:129
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Definition tensor.h:85
GUM_SCALAR mean() const
compute the mean of a numerical discrete random variable @raise
Definition tensor_tpl.h:249
Tensor< GUM_SCALAR > & operator-=(const Tensor< GUM_SCALAR > &r)
the function to be used to add two Tensors
const Tensor< GUM_SCALAR > & translate(GUM_SCALAR v) const
add v to (each value of) *this
Definition tensor_tpl.h:485
Tensor< GUM_SCALAR > & operator/=(const Tensor< GUM_SCALAR > &r)
the function to be used to add two Tensors
Tensor< GUM_SCALAR > sumOut(const gum::VariableSet &del_vars) const
Projection using sum as operation (and implementation-optimized operations).
Definition tensor_tpl.h:497
GUM_SCALAR variance() const
compute the variance of a numerical discrete random variable @raise
Definition tensor_tpl.h:262
Tensor()
Default constructor.
Definition tensor_tpl.h:62
Idx draw() const
get a value at random from a 1-D distribution
Definition tensor_tpl.h:736
GUM_SCALAR entropy() const
entropy of the Tensor
Definition tensor_tpl.h:283
Tensor< GUM_SCALAR > operator|(const Tensor< GUM_SCALAR > &p2) const
the function to be used to add two Tensors
Definition tensor_tpl.h:887
const Tensor< GUM_SCALAR > & log2() const
apply $log_2(x)$ on every element of the container
Definition tensor_tpl.h:379
Tensor< GUM_SCALAR > extract(const Instantiation &inst) const
create a new Tensor extracted from *this given a partial instantiation
Definition tensor_tpl.h:728
const Tensor< GUM_SCALAR > & random() const
generate a random Tensor with each parameter in [0,1]
Definition tensor_tpl.h:785
Tensor< GUM_SCALAR > new_sq() const
Create a new tensor and apply $x^2$ on every element of the container.
Definition tensor_tpl.h:842
Tensor< GUM_SCALAR > isNonZeroMap() const
create a boolean-like tensor using the predicate isNonZero
Definition tensor_tpl.h:637
Tensor< GUM_SCALAR > minOut(const gum::VariableSet &del_vars) const
Projection using min as operation (and implementation-optimized operations).
Definition tensor_tpl.h:541
GUM_SCALAR stdDev() const
compute the stdDev of a numerical discrete random variable
Definition tensor_tpl.h:277
Tensor< GUM_SCALAR > putFirst(const DiscreteVariable *var) const
create a new Tensor with a certain variable in first
Definition tensor_tpl.h:698
Tensor< GUM_SCALAR > maxIn(const gum::VariableSet &kept_vars) const
Projection using max as operation (and implementation-optimized operations).
Definition tensor_tpl.h:624
static Tensor< GUM_SCALAR > evEq(const DiscreteVariable &v, double val)
numerical evidence generator
Tensor< GUM_SCALAR > sumIn(const gum::VariableSet &kept_vars) const
Projection using sum as operation (and implementation-optimized operations).
Definition tensor_tpl.h:585
Tensor< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable * > &vars) const
create a new Tensor with another order
Definition tensor_tpl.h:658
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:772
Tensor< GUM_SCALAR > operator&(const Tensor< GUM_SCALAR > &p2) const
the function to be used to add two Tensors
Definition tensor_tpl.h:902
GUM_SCALAR max() const
max of all elements in the Tensor
Definition tensor_tpl.h:175
Tensor< GUM_SCALAR > * newFactory() const final
Default implementation of MultiDimContainer::set().
Definition tensor_tpl.h:150
const Tensor< GUM_SCALAR > & sgn() const
apply sgn(x)$ on every element of the container
Definition tensor_tpl.h:385
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:852
GUM_SCALAR maxNonOne() const
max of all non one elements in the Tensor
Definition tensor_tpl.h:194
Tensor< GUM_SCALAR > prodIn(const gum::VariableSet &kept_vars) const
Projection using multiplication as operation (and implementation-optimized operations).
Definition tensor_tpl.h:598
bool operator==(const Tensor< GUM_SCALAR > &r) const
the function to be used to add two Tensors
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:847
static Tensor< GUM_SCALAR > uniformTensor(const DiscreteVariable &var)
static Tensor< GUM_SCALAR > evGt(const DiscreteVariable &v, double val)
numerical evidence generator
GUM_SCALAR minNonZero() const
min of all non zero elements in the Tensor
Definition tensor_tpl.h:215
~Tensor() final
Destructor.
Definition tensor_tpl.h:144
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:391
Set< Instantiation > findAll(GUM_SCALAR v) const
set of instantiation corresponding to the parameter v in the Tensor
Definition tensor_tpl.h:758
static Tensor< GUM_SCALAR > evIn(const DiscreteVariable &v, double val1, double val2)
numerical evidence generator
Tensor< GUM_SCALAR > prodOut(const gum::VariableSet &del_vars) const
Projection using multiplication as operation (and implementation-optimized operations).
Definition tensor_tpl.h:519
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:304
Tensor< GUM_SCALAR > maxOut(const gum::VariableSet &del_vars) const
Projection using max as operation (and implementation-optimized operations).
Definition tensor_tpl.h:563
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:828
bool isEvidence() const
is an evidence ? (marginal-like but has not to sum to 1)
Definition tensor_tpl.h:879
Tensor< GUM_SCALAR > & operator=(const Tensor< GUM_SCALAR > &src)
Default constructor.
Definition tensor_tpl.h:124
gum::VariableSet _complementVars_(const gum::VariableSet &del_vars) const
Definition tensor_tpl.h:647
Tensor< GUM_SCALAR > operator/(const Tensor< GUM_SCALAR > &p2) const
the function to be used to divide two Tensors
Definition tensor_tpl.h:963
const Tensor< GUM_SCALAR > & normalizeAsCPT(const Idx &varId=0) const
normalisation of this as a CPT for the variable varId
Definition tensor_tpl.h:447
const Tensor< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
Definition tensor_tpl.h:432
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:837
const Tensor< GUM_SCALAR > & inverse() const
the function to inverse (each value of) *this
Definition tensor_tpl.h:491
Tensor< GUM_SCALAR > & operator<<(const DiscreteVariable &v)
the function to be used to add two Tensors
Definition tensor_tpl.h:857
Tensor< GUM_SCALAR > minIn(const gum::VariableSet &kept_vars) const
Projection using min as operation (and implementation-optimized operations).
Definition tensor_tpl.h:611
Tensor< GUM_SCALAR > operator-(const Tensor< GUM_SCALAR > &p2) const
the function to be used to subtract two Tensors
Definition tensor_tpl.h:930
const Tensor< GUM_SCALAR > & scale(GUM_SCALAR v) const
multiply (each value of) *this by v
Definition tensor_tpl.h:479
GUM_SCALAR min() const
min of all elements in the Tensor
Definition tensor_tpl.h:184
Tensor< GUM_SCALAR > & operator*=(const Tensor< GUM_SCALAR > &r)
the function to be used to add two Tensors
Definition tensor_tpl.h:992
GUM_SCALAR product() const
product of all elements in the Tensor
Definition tensor_tpl.h:166
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:779
const Tensor< GUM_SCALAR > & sq() const
apply $x^2$ on every element of the container
Definition tensor_tpl.h:373
std::string toString() const final
the function to be used to add two Tensors
const Tensor< GUM_SCALAR > & randomCPT() const
generate a random CPT in the Tensor
Definition tensor_tpl.h:810
GUM_SCALAR sum() const
sum of all elements in the Tensor
Definition tensor_tpl.h:157
static Tensor< GUM_SCALAR > evLt(const DiscreteVariable &v, double val)
numerical evidence generator
const Tensor< GUM_SCALAR > & abs() const
Apply abs on every element of the container.
Definition tensor_tpl.h:421
Tensor< GUM_SCALAR > & operator+=(const Tensor< GUM_SCALAR > &r)
the function to be used to add two Tensors
Definition tensor_tpl.h:980
Tensor< GUM_SCALAR > operator+(const Tensor< GUM_SCALAR > &p2) const
the function to be used to add two Tensors
Definition tensor_tpl.h:864
Tensor< GUM_SCALAR > operator~() const
the function to be used to add two Tensors
Definition tensor_tpl.h:917
const Tensor< GUM_SCALAR > & randomDistribution() const
generate a random Distribution in the Tensor
Definition tensor_tpl.h:799
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)
Tensor< GUM_SCALAR > sq(const Tensor< GUM_SCALAR > &arg)
Tensor< GUM_SCALAR > abs(const Tensor< GUM_SCALAR > &arg)
Implementation of the Tensor class.