Namespace nuTensο
-
namespace nuTens
Functions
- constexpr std::complex< float > imagUnit (0.0, 1.0)
-
class BaseMatterSolver
Subclassed by nuTens::ConstDensityMatterSolver
Setters
-
inline virtual BaseMatterSolver &setMixingMatrix(const Tensor &newMatrix)
Set a new mixing matrix for this solver.
- Parameters:
newMatrix β The new matrix to set
-
inline virtual BaseMatterSolver &setMasses(const Tensor &newMasses)
Set new mass eigenvalues for this solver.
- Parameters:
newMasses β The new masses
-
inline virtual BaseMatterSolver &setEnergies(const Tensor &newEnergies)
set new neutrino energies
- Parameters:
newEnergies β new energy values
-
inline virtual BaseMatterSolver &setAntiNeutrino(bool newValue)
Set whether we are dealing with anti-neutrinos.
- Parameters:
newValue β
Unnamed Group
-
inline virtual const Tensor &getMixingMatrix()
getters
Get the current mixing matrix for this solver
-
inline virtual const Tensor &getMasses()
Get the current mass eigenvalues for this solver.
-
inline virtual const Tensor &getEnergies()
-
inline virtual bool getAntiNeutrino()
Get whether we are dealing with anti-neutrinos.
Public Functions
-
inline BaseMatterSolver(int nGenerations, bool antiNeutrino, dtypes::deviceType device)
-
virtual ~BaseMatterSolver() = default
Destructor.
-
BaseMatterSolver(BaseMatterSolver const&) = default
copy constructor
-
BaseMatterSolver &operator=(BaseMatterSolver const&) = default
copy assignment operator
-
BaseMatterSolver(BaseMatterSolver&&) = default
move constructor
-
BaseMatterSolver &operator=(BaseMatterSolver&&) = default
move assignment operator
-
virtual void calculateEigenvalues(EigenvecTensor &eigenvectors, EigenvalTensor &eigenvalues) = 0
-
inline virtual BaseMatterSolver &setMixingMatrix(const Tensor &newMatrix)
-
class BaseMixingMatrix
Subclassed by nuTens::PMNSmatrix
Public Functions
-
inline virtual Tensor &build()
Get the mixing matrix.
-
inline BaseMixingMatrix(dtypes::deviceType device = dtypes::kCPU)
Constructor.
-
virtual ~BaseMixingMatrix() = default
Destructor.
-
BaseMixingMatrix(BaseMixingMatrix const&) = default
copy constructor
-
BaseMixingMatrix &operator=(BaseMixingMatrix const&) = default
copy assignment operator
-
BaseMixingMatrix(BaseMixingMatrix&&) = default
move constructor
-
BaseMixingMatrix &operator=(BaseMixingMatrix&&) = default
move assignment operator
-
inline virtual Tensor &build()
-
class ConstDensityMatterSolver : public nuTens::BaseMatterSolver
Unnamed Group
-
inline float getDensity() const
Getters.
-
virtual void calculateEigenvalues(EigenvecTensor &eigenvectors, EigenvalTensor &eigenvalues) override
Calculate the hamiltonian eigenvalues and eigenvectors, i.e. the effective Mass^2 states and effective mixing matrix.
- Parameters:
eigenvectors β [out] The returned eigenvectors
eigenvalues β [out] The corresponding eigenvalues
-
Tensor getElectronOuterProduct()
construct the outer product of the electron row of the mixing matrix, used in building the hamiltonian, and return a copy of it potentially useful for debugging
-
Tensor getHamiltonian()
construct the hamiltonian and return a copy of it potentially useful for debugging
Setters
-
inline virtual ConstDensityMatterSolver &setAntiNeutrino(bool newValue) override
Set whether we are dealing with anti-neutrinos.
- Parameters:
newValue β
-
inline virtual ConstDensityMatterSolver &setMixingMatrix(const Tensor &newMatrix) override
Set a new mixing matrix for this solver.
- Parameters:
newMatrix β The new matrix to set
-
inline virtual ConstDensityMatterSolver &setMasses(const Tensor &newMasses) override
Set new mass eigenvalues for this solver.
- Parameters:
newMasses β The new masses
-
inline ConstDensityMatterSolver &setDensity(float newDensity)
set a new density
- Parameters:
newDensity β the new value
Public Functions
-
inline ConstDensityMatterSolver(int nGenerations, dtypes::deviceType device = dtypes::kCPU)
Constructor.
nGenerations The number of neutrino generations this propagator should expect
-
~ConstDensityMatterSolver() override = default
destructor
-
ConstDensityMatterSolver(ConstDensityMatterSolver const&) = default
copy constructor
-
ConstDensityMatterSolver &operator=(ConstDensityMatterSolver const&) = default
copy assignment operator
-
ConstDensityMatterSolver(ConstDensityMatterSolver&&) = default
move constructor
-
ConstDensityMatterSolver &operator=(ConstDensityMatterSolver&&) = default
move assignment operator
-
inline float getDensity() const
-
class DPpropagator : public nuTens::Propagator
- #include <DP-propagator.hpp>
Solver based on Denton, Parke (2024) (https://arxiv.org/pdf/2405.02400) assumes 3 flavour oscillations and dm^2_21 > 0.
Unnamed Group
-
inline DPpropagator &setBaseline(float newBaseline)
Setters.
-
inline DPpropagator &setDensity(float newDensity)
-
inline DPpropagator &setTheta12(Tensor &newTheta12)
-
inline DPpropagator &setTheta23(Tensor &newTheta23)
-
inline DPpropagator &setTheta13(Tensor &newTheta13)
-
inline DPpropagator &setDeltaCP(Tensor &newDeltaCP)
-
inline DPpropagator &setDmsq21(Tensor &newDmsq21)
-
inline DPpropagator &setDmsq31(Tensor &newDmsq31)
-
inline DPpropagator &setAntiNeutrino(bool newValue)
-
inline DPpropagator &setSinSquaredThetas(bool newValue)
If true, the \theta_{ij}βs you provide will be interpreted as \sin^2(\theta_{ij}). This will shortcut some of the computations performed by this propagator and speed up calculation time.
-
inline virtual DPpropagator &setEnergies(Tensor &newEnergies) override
Set the neutrino energies.
- Parameters:
newEnergies β The neutrino energies
Unnamed Group
-
inline const Tensor &getTheta12()
Getters.
-
inline const Tensor &getTheta23()
-
inline const Tensor &getTheta13()
-
inline const Tensor &getDeltaCP()
-
inline const Tensor &getDmsp21()
-
inline const Tensor &getDmsq31()
-
inline const Tensor &getEnergies()
-
inline const float &getDensity() const
Public Functions
-
inline DPpropagator(int NRiterations, dtypes::deviceType device = dtypes::kCPU)
-
virtual Tensor calculateProbs() override
Calculate the oscilaltion probabilities for the current set of parameters and energies.
Set a matter solver to use to deal with matter effects.
- Parameters:
newSolver β A derivative of BaseMatterSolver
-
template<typename T = bool>
inline virtual void setMixingMatrix(Tensor &newMatrix) Set a whole new mixing matrix.
- Parameters:
newMatrix β The new matrix to use
-
template<typename T = bool>
inline virtual void setMasses(Tensor &newMasses) Set the masses corresponding to the vacuum hamiltonian eigenstates.
- Parameters:
newMasses β The new masses to use. This tensor is expected to have a batch dimension + 1 more dimensions of size nGenerations. The batch dimension can (and probably should) be 1 and it will be broadcast to match the batch dimension of the energies supplied to calculateProbs(). So dimension should be {1, nGenerations}.
-
inline DPpropagator &setBaseline(float newBaseline)
-
class NoGrad
Public Functions
-
inline NoGrad()
Disable autograd calculations within some scope, improving performance where you are not interested in calculating gradients Instantiate at the start of the scope where you want to disable gradient calculations like so.
#include <nuTens/tensors/tensor.hpp> ... { auto noGrad = nuTens::NoGrad(); ... < speedy non differentiated code > ... }
-
inline NoGrad()
-
class PMNSmatrix : public nuTens::BaseMixingMatrix
- #include <pmns-matrix.hpp>
PMNS matrix in the standard parameterisation Convenient way to construct the matrix.
Unnamed Group
-
inline const Tensor &getTheta12Tensor()
Setters.
-
inline const Tensor &getTheta13Tensor()
-
inline const Tensor &getTheta23Tensor()
-
inline const Tensor &getDeltaCPTensor()
Public Functions
-
inline PMNSmatrix(dtypes::deviceType device = dtypes::kCPU)
-
inline PMNSmatrix &setTheta12(float theta12)
-
inline PMNSmatrix &setTheta13(float theta13)
-
inline PMNSmatrix &setTheta23(float theta23)
-
inline PMNSmatrix &setDeltaCP(float deltaCP)
-
inline const Tensor &getTheta12Tensor()
-
class Propagator
Subclassed by nuTens::DPpropagator
Setters
-
inline Propagator &setAntiNeutrino(bool newValue)
Set whether we are dealing with anti-neutrinos.
- Parameters:
newValue β
Set a matter solver to use to deal with matter effects.
- Parameters:
newSolver β A derivative of BaseMatterSolver
-
inline virtual Propagator &setEnergies(Tensor &newEnergies)
Set the neutrino energies.
- Todo:
Should add a check to tensors supplied to the setters to see how many dimensions they have, and if missing a batch dimension, add one.
- Parameters:
newEnergies β The neutrino energies
-
inline virtual Propagator &setMasses(Tensor &newMasses)
Set the masses corresponding to the vacuum hamiltonian eigenstates.
- Parameters:
newMasses β The new masses to use. This tensor is expected to have a batch dimension + 1 more dimensions of size nGenerations. The batch dimension can (and probably should) be 1 and it will be broadcast to match the batch dimension of the energies supplied to calculateProbs(). So dimension should be {1, nGenerations}.
-
inline virtual Propagator &setMixingMatrix(Tensor &newMatrix)
Set a whole new mixing matrix.
- Parameters:
newMatrix β The new matrix to use
-
inline Propagator &setBaseline(float newBaseline)
Set the baseline.
- Parameters:
newBaseline β new value
Unnamed Group
-
inline float getBaseline() const
Getters.
Public Functions
-
inline Propagator(int nGenerations, dtypes::deviceType device = dtypes::kCPU)
Constructor.
- Parameters:
nGenerations β The number of generations the propagator should expect
-
virtual ~Propagator() = default
Destructor.
-
Propagator(Propagator const&) = default
copy constructor
-
Propagator &operator=(Propagator const&) = default
copy assignment operator
-
Propagator(Propagator&&) = default
move constructor
-
Propagator &operator=(Propagator&&) = default
move assignment operator
-
virtual Tensor calculateProbs()
Calculate the oscillation probabilities.
- Parameters:
energies β The energies of the neutrinos
-
inline Propagator &setAntiNeutrino(bool newValue)
-
class Tensor
Subclassed by nuTens::BaseMatterSolver::EigenvalTensor, nuTens::BaseMatterSolver::EigenvecTensor
Constructors
Use these methods to construct tensors
-
inline Tensor()
Default constructor with no initialisation.
-
Tensor(const std::vector<float> &values, dtypes::scalarType type = dtypes::kFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = false)
Construct a 1-d array with specified values.
values The values to include in the tensor
-
virtual ~Tensor() = default
Destructor.
-
Tensor(Tensor const&) = default
copy constructor
-
Tensor(Tensor&&) = default
move constructor
-
static Tensor TensorComplex(const std::vector<std::complex<float>> &values, dtypes::scalarType type = dtypes::kComplexFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = false)
Construct a 1-d array with specified complex values.
values The values to include in the tensor
Warning
This can be quite slow due to internal conversions between complex types. Avoid using it for anything performance critical!!!
-
static Tensor eye(int n, dtypes::scalarType type = dtypes::kFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = false)
Construct an identity tensor (has to be a 2d square tensor)
n The size of one of the sides of the tensor
type The data type of the tensor
-
static Tensor rand(const std::vector<long int> &shape, dtypes::scalarType type = dtypes::kFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = false)
Construct a tensor with entries randomly initialised in the range [0, 1].
shape The desired shape of the intitalised tensor
type The data type of the tensor
-
static Tensor diag(const Tensor &diag)
Construct a tensor diag values along the diagonal, and zero elsewhere.
diag A 1-d tensor which represents the desired diagonal values
-
static Tensor ones(const std::vector<long int> &shape, dtypes::scalarType type = dtypes::kFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = false)
Construct a tensor with ones.
shape The desired shape of the intitalised tensor
type The data type of the tensor
-
static Tensor zeros(const std::vector<long int> &shape, dtypes::scalarType type = dtypes::kFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = false)
Construct a tensor with zeros.
shape The desired shape of the intitalised tensor
type The data type of the tensor
Setters
Set the underlying data type of this tensor
-
Tensor &dType(dtypes::scalarType type)
-
Tensor &device(dtypes::deviceType device)
Set the device that this tensor lives on.
-
Tensor &requiresGrad(bool reqGrad)
Set whether the tensor requires a gradient.
-
inline Tensor &hasBatchDim(bool hasBatchDim)
Set whether or not the first dimension should be interpreted as a batch dimension.
Getters
The underlying data type of this tensor
-
inline dtypes::scalarType getDType() const
-
inline dtypes::deviceType getDevice() const
The device that this tensor lives on.
-
inline bool getRequiresGrad() const
Whether the tensor requires a gradient.
-
inline bool isInitialised() const
Check if tensor has been initialised.
Matrix Arithmetic
Generally there are static functions with the pattern <function>(Mat1, Mat2) which will return a new matrix and inline equivalents with the pattern <function>_(Mat2) which will affect the object they are called by
-
void matmul_(const Tensor &tensor2)
Inplace matrix multiplication.
tensor2 Right hand matrix to multiply with this one
-
void mul_(const Tensor &tensor2)
Inplace element-wise multiplication.
tensor2 Right hand tensor
-
void div_(const Tensor &tensor2)
Inplace element-wise division.
tensor2 Denominator
-
void scale_(float scalar)
Inplace matrix scaling.
scalar The scalar
-
void scale_(std::complex<float> scalar)
Inplace complex matrix scaling.
scalar The scalar
-
void pow_(float scalar)
Inplace raise to scalar power.
scalar The scalar
-
void pow_(std::complex<float> scalar)
Inplace raise to scalar power.
scalar The scalar
-
void exp_()
Inplace element-wise exponential.
-
void transpose_(int dim0, int dim1)
Inplace transpose.
dim0 The first dimension to swap
dim1 The second dimension to swap
-
static Tensor matmul(const Tensor &tensor1, const Tensor &tensor2)
Multiply two matrices together.
tensor1 Left hand tensor
tensor2 Right hand tensor
-
static Tensor outer(const Tensor &tensor1, const Tensor &tensor2)
Outer product of two 1D tensors.
tensor1 Left hand tensor
tensor2 Right hand tensor
-
static Tensor mul(const Tensor &tensor1, const Tensor &tensor2)
Element-wise multiplication of two tensors.
tensor1 Left hand tensor
tensor2 Right hand tensor
-
static Tensor add(const Tensor &tensor1, const Tensor &tensor2)
Element-wise addition of two tensors.
tensor1 Left hand tensor
tensor2 Right hand tensor
-
static Tensor div(const Tensor &tensor1, const Tensor &tensor2)
Element-wise division of two tensors.
tensor1 Numerator
tensor2 Denominator
-
static Tensor pow(const Tensor &tensor, float scalar)
Raise a matrix to a scalar power.
tensor The tensor
scalar The scalar
-
static Tensor pow(const Tensor &tensor, double scalar)
Raise a matrix to a scalar power.
tensor The tensor
scalar The scalar
-
static Tensor pow(const Tensor &tensor, std::complex<float> scalar)
Raise a matrix to a scalar power.
tensor The tensor
scalar The scalar
-
static Tensor pow(const Tensor &tensor, std::complex<double> scalar)
Raise a matrix to a scalar power.
tensor The tensor
scalar The scalar
-
static inline Tensor square(const Tensor &tensor)
Element wise square, slightly faster than pow(tensor, 2.0)
tensor The tensor
-
static Tensor transpose(const Tensor &tensor, int dim0, int dim1)
Get the transpose of a tensor.
tensor The tensor
dim0 The first dimension to swap
dim1 The second dimension to swap
-
static Tensor scale(const Tensor &tensor, float scalar)
Scale a matrix by some scalar.
scalar The scalar
tensor The tensor
-
static Tensor scale(const Tensor &tensor, double scalar)
Scale a matrix by some scalar.
scalar The scalar
tensor The tensor
Mathematical
mathematical function overrides, generally work as expected, unless otherwise noted
-
bool operator==(const Tensor &rhs) const
-
bool operator!=(const Tensor &rhs) const
-
Tensor operator+(double rhs) const
-
Tensor operator-(double rhs) const
-
Tensor operator*(double rhs) const
-
Tensor operator/(double rhs) const
-
Tensor operator-() const
Gradients
-
void backward() const
Compute gradients of this tensor with respect to leaves Those can then be accessed using gradient()
-
void zeroGrad()
Set the accumulated gradient for this tensor to zero.
Warning
This should be done any time you reuse a leaf tensor to calculate another gradient. Otherwise you will get the sum of all accumulated gradients
-
Tensor grad() const
Return a tensor containing the accumulated gradients calculated for this tensor after calling backward()
Linear Algebra
-
static void eig(const Tensor &tensor, Tensor &eVals, Tensor &eVecs)
Get eigenvalues and vectors of a tensor ordering of the eigenvalues is not guarenteed for eigh!!! AAARRRGGHHH.
- Todo:
: figure out a way to deal with that!
tensor The tensor
- Parameters:
eVals β [out] The eigenvalues
eVecs β [out] The eigenvectors
-
static void eigh(const Tensor &tensor, Tensor &eVals, Tensor &eVecs)
Get eigenvalues and vectors of a hermitian matrix.
tensor The tensor
- Parameters:
eVals β [out] The eigenvalues
eVecs β [out] The eigenvectors This is in general faster and more stable than Tensor::eig and should be preferred in basically all cases where it can be used
-
static void eigvals(const Tensor &tensor, Tensor &eVals)
Get eigenvalues of a tensor ordering of the eigenvalues is not guarenteed for eigh!!! AAARRRGGHHH.
- Todo:
: figure out a way to deal with that!
tensor The tensor
- Parameters:
eVals β [out] The eigenvalues
-
static void eigvalsh(const Tensor &tensor, Tensor &eVals)
Get eigenvalues of a hermitian matrix.
tensor The tensor
- Parameters:
eVals β [out] The eigenvalues This is in general faster and more stable than Tensor::eigvals and should be preferred in basically all cases where it can be used
Trigonometric
Public Types
Public Functions
-
Tensor &addBatchDim()
If the tensor does not already have a batch dimension (as set by hasBatchDim()) this will add one.
-
Tensor &unsqueeze(int index)
add new dimension to the tensor at a particular index
-
Tensor real() const
Get the real part of a complex tensor.
-
Tensor imag() const
Get the imaginary part of a complex tensor.
-
Tensor conj() const
Get the complex conjugate of this tensor. If the underlying tensor is not complex, this will just return the tensor.
-
Tensor abs() const
Get elementwise absolute magnitude of a complex tensor.
-
Tensor angle() const
Get elementwise phases of a complex tensor.
-
Tensor cumsum(int dim) const
Get the cumulative sum over some dimension.
- Parameters:
dim β The dimension to sum over
-
Tensor sum() const
Get the result of summing this tensor over all dimensions.
-
Tensor sum(const std::vector<long int> &dims) const
Get the result of summing this tensor over all dimensions.
- Parameters:
dims β The dimensions to sum over
-
std::string toString() const
Print this object to a summary string.
-
void setValue(const std::vector<indexType> &indices, const Tensor &value)
Set the value at a particular index of the tensor.
indices The indices of the value to set
value The value to set it to
-
virtual void setValue(const std::vector<int> &indices, float value)
-
virtual void setValue(const std::vector<int> &indices, double value)
-
Tensor getValues(const std::vector<indexType> &indices) const
Get the value at a certain entry in the tensor.
- Parameters:
indices β The index of the entry to get
-
variantType getVariantValue(const std::vector<int> &indices) const
Get the value at a certain entry in the tensor as an std::variant.
This mainly exists so we can get the values of a tensor in python as pybind11 DOES NOT like templated functions If using the c++ interface it is probably easier, faster and safer to use the templated getValue() function.
-
size_t getNdim() const
Get the number of dimensions in the tensor.
-
int getBatchDim() const
Get the size of the batch dimension of the tensor.
-
bool getHasBatchDim() const
Check if tensor has batch dimension prepended.
-
std::vector<long int> getShape() const
Get the shape of the tensor.
Warning
This can be somewhat slow, you should avoid using it in any performance critical loops
Public Static Functions
-
static bool gpuAvailable()
Check if there is a gpu available to use.
-
static inline Tensor cumsum(const Tensor &tensor, int dim)
Get the cumulative sum over some dimension.
- Parameters:
dim β The dimension to sum over
-
static inline Tensor sum(const Tensor &tensor)
Get the result of summing this tensor over all dimensions.
-
static inline Tensor sum(const Tensor &tensor, const std::vector<long int> &dims)
Get the result of summing this tensor over all dimensions.
- Parameters:
dims β The dimensions to sum over
-
static std::string getTensorLibrary()
Get the name of the backend library used to deal with tensors.
-
inline Tensor()
-
namespace constants
-
namespace dtypes
Enums
-
enum scalarType
Types of scalar values.
Values:
-
enumerator kFloat
-
enumerator kDouble
-
enumerator kComplexFloat
-
enumerator kComplexDouble
-
enumerator kUninitScalar
-
enumerator kFloat
-
enum deviceType
Devices that a Tensor can live on.
Values:
-
enumerator kCPU
-
enumerator kGPU
-
enumerator kUninitDevice
-
enumerator kCPU
Functions
-
template<typename T>
static constexpr scalarType scalarTypeFromRaw() map between raw c++ types and the data types used in nuTens
-
template<>
constexpr scalarType scalarTypeFromRaw<float>()
-
template<>
constexpr scalarType scalarTypeFromRaw<double>()
-
template<>
constexpr scalarType scalarTypeFromRaw<std::complex<float>>()
-
template<>
constexpr scalarType scalarTypeFromRaw<std::complex<double>>()
-
enum scalarType
-
namespace units