#ifndef _melder_tensor_h_
#define _melder_tensor_h_
/* melder_tensor.h
*
* Copyright (C) 1992-2026 Paul Boersma
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This code is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this work. If not, see .
*/
#pragma mark - TENSOR
/*
Base-1 tensors, for parallellism with the scripting language.
Initialization (tested in praat.cpp):
VEC x; // initializes x.cells to nullptr and x.size to 0
double a [] = { 3.14, 2.718 };
VEC x3 (a, 2); // initializes x3 to 100 values from a base-0 array
Melder_assert (x3 [2] == 2.718);
constVEC x4 = { 3.14, 2.718 };
Melder_assert (x4 [2] == 2.718);
autoVEC y; // initializes y.cells to nullptr and y.size to 0
autoVEC y2 = zero_VEC (100); // initializes y to 100 zeroes, having ownership
autoVEC y1 = raw_VEC (100); // initializes y to 100 uninitialized values (caution!), having ownership
y.adoptFromAmbiguousOwner (x); // initializes y to the content of x, taking ownership (explicit, so not "y = x")
VEC z = y.releaseToAmbiguousOwner(); // releases ownership, y.cells becoming nullptr
"}" // end of scope destroys y.cells if not nullptr
autoVEC z1 = y2.move() // moves the content of y2 to z1, emptying y2
To return an autoVEC from a function, transfer ownership like this:
autoVEC foo () {
autoVEC x = zero_VEC (100);
... // fill in the 100 values
return x;
}
*/
/*
Forward declarations, needed because of all the conversions and deletions.
*/
template class vector;
template class constvector;
template class vectorview;
template class constvectorview;
template class autovector;
template class matrix;
template class constmatrix;
template class matrixview;
template class constmatrixview;
template class automatrix;
template
struct vector {
T *cells = nullptr;
integer size = 0;
vector ()
= default;
explicit vector (T *givenCells, integer givenSize)
: cells (givenCells), size (givenSize) { }
explicit vector (matrix const& mat)
: vector (mat.cells, mat.nrow * mat.ncol) { }
inline T& operator[] (integer i) const {
return our cells [i - 1];
}
/*
part (first, last) should crash under the exact
same conditions as a rising for-loop over the elements
from first to last (or a falling for-loop over
the elements from last to first) should crash.
*/
vector part (integer first, integer last) const {
const integer newSize = last - (first - 1);
/*
for-loops don't crash if the number of elements is zero.
*/
if (newSize <= 0)
return vector ();
Melder_assert (first >= 1 && first <= our size);
Melder_assert (last >= 1 && last <= our size);
return vector (& our cells [first - 1], newSize);
}
matrix asmatrix (integer nrow, integer ncol) {
Melder_assert (nrow * ncol <= our size);
return matrix (our cells, nrow, ncol);
}
T *begin () const { return our cells; }
T *end () const { return our cells + our size; }
T *asArgumentToFunctionThatExpectsZeroBasedArray () const { return our cells; }
T *asArgumentToFunctionThatExpectsOneBasedArray () const { return our cells - 1; }
};
template
struct constvector {
const T *cells = nullptr;
integer size = 0;
constvector ()
= default;
explicit constvector (const T *givenCells, integer givenSize)
: cells (givenCells), size (givenSize) { }
explicit constvector (constmatrix const& mat)
: constvector (mat.cells, mat.nrow * mat.ncol) { }
constvector (vector const& other)
: constvector (other.cells, other.size) { }
const T& operator[] (integer i) const { // it's still a reference, because we need to be able to take its address
return our cells [i - 1];
}
constvector part (integer first, integer last) const {
const integer newSize = last - (first - 1);
if (newSize <= 0)
return constvector ();
Melder_assert (first >= 1 && first <= our size);
Melder_assert (last >= 1 && last <= our size);
return constvector (& our cells [first - 1], newSize);
}
constmatrix asmatrix (integer nrow, integer ncol) {
Melder_assert (nrow * ncol <= our size);
return constmatrix (our cells, nrow, ncol);
}
const T *begin () const { return our cells; }
const T *end () const { return our cells + our size; }
const T *asArgumentToFunctionThatExpectsZeroBasedArray () const { return our cells; }
const T *asArgumentToFunctionThatExpectsOneBasedArray () const { return our cells - 1; }
};
template
struct vectorview {
T * firstCell = nullptr;
integer size = 0;
integer stride = 1;
vectorview ()
= default;
explicit vectorview (T *const firstCell_, integer const size_, integer const stride_)
: firstCell (firstCell_), size (size_), stride (stride_) { }
vectorview (vector const& other)
: firstCell (other.cells), size (other.size), stride (1) { }
T& operator[] (integer i) const {
return our firstCell [(i - 1) * our stride];
}
vectorview part (integer first, integer last) const {
const integer newSize = last - (first - 1);
if (newSize <= 0)
return vectorview ();
Melder_assert (first >= 1 && first <= our size);
Melder_assert (last >= 1 && last <= our size);
return vectorview (& our operator[] (first), newSize, our stride);
}
matrixview asmatrixview (integer nrow, integer ncol) {
Melder_assert (nrow * ncol <= our size);
return matrixview (our firstCell, nrow, ncol, ncol * our stride, our stride);
}
T *asArgumentToFunctionThatExpectsZeroBasedArray () const { return & our operator[] (1); }
T *asArgumentToFunctionThatExpectsOneBasedArray () const { return & our operator[] (0); }
};
template
struct constvectorview {
const T * firstCell = nullptr;
integer size = 0;
integer stride = 1;
constvectorview () = default;
explicit constvectorview (const T *const firstCell_, integer const size_, integer const stride_)
: firstCell (firstCell_), size (size_), stride (stride_) { }
constvectorview (constvector const& other)
: constvectorview (other.cells, other.size, 1) { }
constvectorview (vectorview const& other)
: constvectorview (other.firstCell, other.size, other.stride) { }
constvectorview (vector const& other)
: constvectorview (other.cells, other.size, 1) { }
T const& operator[] (integer i) const {
return our firstCell [(i - 1) * our stride];
}
constvectorview part (integer first, integer last) const {
const integer newSize = last - (first - 1);
if (newSize <= 0)
return constvectorview ();
Melder_assert (first >= 1 && first <= our size);
Melder_assert (last >= 1 && last <= our size);
return constvectorview (& our operator[] (first), newSize, our stride);
}
constmatrixview asmatrixview (integer nrow, integer ncol) {
Melder_assert (nrow * ncol <= our size);
return constmatrixview (our firstCell, nrow, ncol, ncol * our stride, our stride);
}
const T *asArgumentToFunctionThatExpectsZeroBasedArray () const { return & our operator[] (1); }
const T *asArgumentToFunctionThatExpectsOneBasedArray () const { return & our operator[] (0); }
};
/*
An autovector is the sole owner of its payload, which is a vector.
When the autovector ends its life (goes out of scope),
it should destroy its payload (if it has not sold it),
because keeping a payload alive when the owner is dead
would continue to use some of the computer's resources (namely, memory).
*/
template
struct autovector {
T *cells = nullptr;
integer size = 0;
integer _capacity = 0;
autovector () // come into existence without a payload
= default;
explicit autovector (integer givenSize, MelderArray::kInitializationType initializationType) { // come into existence and manufacture a payload
Melder_assert (givenSize >= 0);
our cells = MelderArray:: _alloc (givenSize, initializationType);
our size = givenSize;
our _capacity = givenSize;
}
autovector (std::initializer_list list) {
our size = uinteger_to_integer_a (list.size());
our cells = MelderArray:: _alloc (our size, MelderArray::kInitializationType::RAW); // raw is possible because T is copyable data
T *p = our cells;
for (auto cell : list)
* (p ++) = cell;
}
void reset () noexcept { // on behalf of ambiguous owners (otherwise this could be in autovector<>)
if (our cells) {
MelderArray:: _free (our cells, our _capacity);
our cells = nullptr;
}
our size = 0;
our _capacity = 0;
}
~autovector () { // destroy the payload (if any)
our reset ();
}
T& operator[] (integer i) const {
return our cells [i - 1];
}
vector get () const { return vector (our cells, our size); } // let the public use the payload (they may change the values of the elements but not the at-pointer or the size)
vectorview all () const { return vectorview (our cells, our size, 1); }
void adoptFromAmbiguousOwner (vector given) { // buy the payload from a non-autovector
our reset();
our cells = given.cells;
our size = given.size;
our _capacity = given.size;
}
vector releaseToAmbiguousOwner () { // sell the payload to a non-autovector
T *oldCells = our cells;
our cells = nullptr; // disown ourselves, preventing automatic destruction of the payload
integer oldSize = our size;
our size = 0;
our _capacity = 0;
return vector (oldCells, oldSize);
}
vector part (integer first, integer last) const {
const integer newSize = last - (first - 1);
/*
for-loops don't crash if the number of elements is zero.
*/
if (newSize <= 0)
return vector ();
Melder_assert (first >= 1 && first <= our size);
Melder_assert (last >= 1 && last <= our size);
return vector (& our cells [first - 1], newSize);
}
T *begin () const { return our cells; }
T *end () const { return our cells + our size; }
T *asArgumentToFunctionThatExpectsZeroBasedArray () const { return our cells; }
T *asArgumentToFunctionThatExpectsOneBasedArray () const { return our cells - 1; }
/*
Disable copying via construction or assignment (which would violate unique ownership of the payload).
*/
autovector (autovector const& other) = delete; // disable copy construction
autovector& operator= (autovector const& other) = delete; // disable copy assignment
/*
Enable moving of r-values (temporaries, implicitly) or l-values (for variables, via an explicit move()).
This implements buying a payload from another autovector (which involves destroying our current payload).
*/
autovector (autovector&& other) noexcept : cells (other.cells), size (other.size), _capacity (other._capacity) { // enable move construction
other.cells = nullptr; // disown source
other.size = 0; // to keep the source in a valid state
other._capacity = 0;
}
autovector& operator= (autovector&& other) noexcept { // enable move assignment
if (other.cells != our cells) {
our reset ();
our cells = other.cells;
our size = other.size;
our _capacity = other._capacity;
other.cells = nullptr; // disown source
other.size = 0; // to keep the source in a valid state
other._capacity = 0;
}
return *this;
}
autovector&& move () noexcept { return static_cast (*this); } // enable construction and assignment for l-values (variables) via explicit move()
/*
Some of the following functions are capable of keeping a valid `cells` pointer
while `size` can at the same time be zero.
*/
void initWithCapacity (integer capacity, MelderArray::kInitializationType initializationType = MelderArray::kInitializationType::ZERO) {
if (capacity > 0)
our cells = MelderArray:: _alloc (capacity, initializationType);
our size = 0;
our _capacity = capacity;
}
/*
If the new size N is less than the current size S,
then the first N elements of the vector are kept,
so if you want to keep a different range than the
first N elements of your original vector,
you should shift the elements before resizing.
If the new size N is greater than the current size S,
then all S elements of the vector are kept,
and they are the first S elements of the new vector
the remaining S - N elements may be initialized to zero.
If you want the original S elements to show up
elsewhere than at the head of the vector,
you should shift the elements after resizing.
*/
void resize (integer newSize, MelderArray::kInitializationType initializationType = MelderArray::kInitializationType::ZERO) {
if (newSize > our _capacity) {
/*
The new capacity is at least twice the old capacity.
When starting at a capacity of 0, and continually upsizing by one,
the capacity sequence will be: 0, 11, 33, 77, 165, 341, 693, 1397,
2805, 5621, 11253, 22517, 45045, 90101, 180213, 360437, 720885...
*/
integer newCapacity = newSize + our size + 10;
/*
Create without change.
*/
T *newCells = MelderArray:: _alloc (newCapacity, initializationType);
/*
Change without error.
*/
for (integer i = 1; i <= our size; i ++)
newCells [i - 1] = std::move (our cells [i - 1]);
if (our cells)
MelderArray:: _free (our cells, our _capacity);
our cells = newCells;
our _capacity = newCapacity;
}
our size = newSize;
}
void insert (integer position, const T& value) {
if (position == 0)
position = our size + 1;
resize (our size + 1, MelderArray::kInitializationType::RAW);
Melder_assert (position >= 1 && position <= our size);
for (integer i = our size; i > position; i --)
our cells [i - 1] = std::move (our cells [i - 2]);
our cells [position - 1] = value;
}
T* append () {
resize (our size + 1, MelderArray::kInitializationType::ZERO);
return & our cells [our size - 1];
}
void remove (integer position) {
Melder_assert (position >= 1 && position <= our size);
for (integer i = position; i < our size; i ++)
our cells [i - 1] = std::move (our cells [i]);
resize (our size - 1);
}
};
template
autovector newvectorraw (integer size) {
return autovector (size, MelderArray::kInitializationType::RAW);
}
template
autovector newvectorzero (integer size) {
return autovector (size, MelderArray::kInitializationType::ZERO);
}
template
autovector newvectorcopy (constvectorview source) {
autovector result = newvectorraw (source.size);
for (integer i = 1; i <= source.size; i ++)
result [i] = source [i];
return result;
}
template
autovector newvectorcopy (vectorview source) {
return newvectorcopy (constvectorview (source));
}
template
struct matrix {
T *cells = nullptr;
integer nrow = 0, ncol = 0;
matrix ()
= default;
explicit matrix (T *givenCells, integer givenNrow, integer givenNcol)
: cells (givenCells), nrow (givenNrow), ncol (givenNcol) { }
//matrix (matrix const& other)
// = default;
//matrix& operator= (matrix const& other)
// = default;
explicit matrix (vector const& vec, integer givenNrow, integer givenNcol)
: matrix (vec.cells, givenNrow, givenNcol)
{
Melder_assert (givenNrow * givenNcol <= vec.size);
}
vector operator[] (integer rowNumber) const {
return vector (our cells + (rowNumber - 1) * our ncol, our ncol);
}
vector row (integer rowNumber) const {
Melder_assert (rowNumber >= 1 && rowNumber <= our nrow);
Melder_assert (our cells);
return vector (our cells + (rowNumber - 1) * our ncol, our ncol);
}
vectorview column (integer columnNumber) const {
Melder_assert (columnNumber >= 1 && columnNumber <= our ncol);
return vectorview (our cells + (columnNumber - 1), our nrow, our ncol);
}
vectorview diagonal () const {
return vectorview (our cells, std::min (our nrow, our ncol), our ncol + 1);
}
matrixview horizontalBand (integer firstRow, integer lastRow) const {
const integer newNrow = lastRow - (firstRow - 1);
if (newNrow <= 0)
return matrixview ();
Melder_assert (firstRow >= 1 && firstRow <= our nrow);
Melder_assert (lastRow >= 1 && lastRow <= our nrow);
return matrixview (our cells + (firstRow - 1) * our ncol, newNrow, our ncol, our ncol, 1);
}
matrixview verticalBand (integer firstColumn, integer lastColumn) const {
const integer newNcol = lastColumn - (firstColumn - 1);
if (newNcol <= 0)
return matrixview ();
Melder_assert (firstColumn >= 1 && firstColumn <= our ncol);
Melder_assert (lastColumn >= 1 && lastColumn <= our ncol);
return matrixview (our cells + (firstColumn - 1), our nrow, newNcol, our ncol, 1);
}
matrixview part (integer firstRow, integer lastRow, integer firstColumn, integer lastColumn) const {
const integer newNrow = lastRow - (firstRow - 1), newNcol = lastColumn - (firstColumn - 1);
if (newNrow <= 0 || newNcol <= 0)
return matrixview ();
Melder_assert (firstRow >= 1 && firstRow <= our nrow);
Melder_assert (lastRow >= 1 && lastRow <= our nrow);
Melder_assert (firstColumn >= 1 && firstColumn <= our ncol);
Melder_assert (lastColumn >= 1 && lastColumn <= our ncol);
return matrixview (
our cells + (firstRow - 1) * our ncol + (firstColumn - 1),
newNrow, newNcol, our ncol, 1
);
}
matrixview transpose () const {
return matrixview (our cells, our ncol, our nrow, 1, our ncol);
}
vector asvector () const {
return vector (our cells, our nrow * our ncol);
}
vector asvector (integer size) const {
Melder_assert (size <= our nrow * our ncol);
return vector (our cells, size);
}
};
template
struct constmatrix {
const T *cells = nullptr;
integer nrow = 0, ncol = 0;
constmatrix ()
= default;
explicit constmatrix (const T *givenCells, integer givenNrow, integer givenNcol)
: cells (givenCells), nrow (givenNrow), ncol (givenNcol) { }
constmatrix (matrix const& other)
: constmatrix (other.cells, other.nrow, other.ncol) { }
explicit constmatrix (vector const& vec, integer givenNrow, integer givenNcol)
: constmatrix (vec.cells, givenNrow, givenNcol)
{
Melder_assert (givenNrow * givenNcol <= vec.size);
}
constvector operator[] (integer rowNumber) const {
return constvector (our cells + (rowNumber - 1) * our ncol, our ncol);
}
constvector row (integer rowNumber) const {
Melder_assert (rowNumber >= 1 && rowNumber <= our nrow);
Melder_assert (our cells);
return constvector (our cells + (rowNumber - 1) * our ncol, our ncol);
}
constvectorview column (integer columnNumber) const {
Melder_assert (columnNumber >= 1 && columnNumber <= our ncol);
return constvectorview (our cells + (columnNumber - 1), our nrow, our ncol);
}
constvectorview diagonal () const {
return constvectorview (our cells, std::min (our nrow, our ncol), our ncol + 1);
}
constmatrixview horizontalBand (integer firstRow, integer lastRow) const {
const integer newNrow = lastRow - (firstRow - 1);
if (newNrow <= 0)
return constmatrixview ();
Melder_assert (firstRow >= 1 && firstRow <= our nrow);
Melder_assert (lastRow >= 1 && lastRow <= our nrow);
return constmatrixview (our cells + (firstRow - 1) * our ncol, newNrow, our ncol, our ncol, 1);
}
constmatrixview verticalBand (integer firstColumn, integer lastColumn) const {
const integer newNcol = lastColumn - (firstColumn - 1);
if (newNcol <= 0)
return constmatrixview ();
Melder_assert (firstColumn >= 1 && firstColumn <= our ncol);
Melder_assert (lastColumn >= 1 && lastColumn <= our ncol);
return constmatrixview (our cells + (firstColumn - 1), our nrow, newNcol, our ncol, 1);
}
constmatrixview part (integer firstRow, integer lastRow, integer firstColumn, integer lastColumn) const {
const integer newNrow = lastRow - (firstRow - 1), newNcol = lastColumn - (firstColumn - 1);
if (newNrow <= 0 || newNcol <= 0)
return constmatrixview ();
Melder_assert (firstRow >= 1 && firstRow <= our nrow);
Melder_assert (lastRow >= 1 && lastRow <= our nrow);
Melder_assert (firstColumn >= 1 && firstColumn <= our ncol);
Melder_assert (lastColumn >= 1 && lastColumn <= our ncol);
return constmatrixview (
our cells + (firstRow - 1) * our ncol + (firstColumn - 1),
newNrow, newNcol, our ncol, 1
);
}
constmatrixview transpose () const {
return constmatrixview (our cells, our ncol, our nrow, 1, our ncol);
}
constvector asvector () const {
return constvector (our cells, our nrow * our ncol);
}
constvector asvector (integer size) const {
Melder_assert (size <= our nrow * our ncol);
return constvector (our cells, size);
}
};
template
struct matrixview {
T * firstCell = nullptr;
integer nrow = 0, ncol = 0;
/*mutable*/ integer rowStride = 0, colStride = 1; // mutable perhaps once an automatrix has strides
/*
Make sure that each of the following creates an appropriately initialized matrixview:
matrixview matvu; // OK
auto matvu = matrixview(); // OK
*/
matrixview ()
= default;
/*
The following constructor is explicit, i.e.,
it cannot be used as an implicit conversion from an initializer list,
as in any of the following:
matrixview mat = { p, 10, 100, 100, 1 }; // WRONG
myFunction ({ p, 10, 100, 100, 1 }); // WRONG
whereas any of the following is fine:
matrixview mat { p, 10, 100, 100, 1 }; // OK
matrixview mat (p, 10, 100, 100, 1); // OK
auto mat = matrixview { p, 10, 100, 100, 1 }; // OK
auto mat = matrixview (p, 10, 100, 100, 1); // OK
myFunction (matrixview { p, 10, 100, 100, 1 }); // OK
myFunction (matrixview (p, 10, 100, 100, 1)); // OK
*/
explicit matrixview (T * firstCell_, integer nrow_, integer ncol_, integer rowStride_, integer colStride_)
: firstCell (firstCell_), nrow (nrow_), ncol (ncol_), rowStride (rowStride_), colStride (colStride_) { }
/*
The following constructor is implicit, i.e.,
you can assign a matrix to a matrixview.
*/
matrixview (matrix const& other)
: matrixview (other.cells, other.nrow, other.ncol, other.ncol, 1_integer) { }
explicit matrixview (vectorview const& vec, integer givenNrow, integer givenNcol) :
matrixview (vec.cells, givenNrow, givenNcol, givenNcol * vec.stride, vec.stride)
{
Melder_assert (givenNrow * givenNcol <= vec.size);
}
vectorview operator[] (integer rowNumber) const {
return vectorview (our firstCell + (rowNumber - 1) * our rowStride, our ncol, our colStride);
}
vectorview row (integer rowNumber) const {
Melder_assert (rowNumber >= 1 && rowNumber <= our nrow);
return vectorview (our firstCell + (rowNumber - 1) * our rowStride, our ncol, our colStride);
}
vectorview column (integer columnNumber) const {
Melder_assert (columnNumber >= 1 && columnNumber <= our ncol);
return vectorview (our firstCell + (columnNumber - 1) * our colStride, our nrow, our rowStride);
}
vectorview diagonal () const {
return vectorview (our firstCell, std::min (our nrow, our ncol), our rowStride + our colStride);
}
matrixview verticalBand (integer firstColumn, integer lastColumn) const {
const integer newNcol = lastColumn - (firstColumn - 1);
if (newNcol <= 0)
return matrixview ();
Melder_assert (firstColumn >= 1 && firstColumn <= our ncol);
Melder_assert (lastColumn >= 1 && lastColumn <= our ncol);
return matrixview (our firstCell + (firstColumn - 1) * our colStride,
our nrow, newNcol, our rowStride, our colStride);
}
matrixview part (integer firstRow, integer lastRow, integer firstColumn, integer lastColumn) const {
const integer newNrow = lastRow - (firstRow - 1), newNcol = lastColumn - (firstColumn - 1);
if (newNrow <= 0 || newNcol <= 0)
return matrixview ();
Melder_assert (firstRow >= 1 && firstRow <= our nrow);
Melder_assert (lastRow >= 1 && lastRow <= our nrow);
Melder_assert (firstColumn >= 1 && firstColumn <= our ncol);
Melder_assert (lastColumn >= 1 && lastColumn <= our ncol);
return matrixview (
our firstCell + (firstRow - 1) * our rowStride + (firstColumn - 1) * our colStride,
newNrow, newNcol, our rowStride, our colStride
);
}
matrixview transpose () const {
return matrixview (our firstCell, our ncol, our nrow, our colStride, our rowStride);
}
};
template
struct constmatrixview {
const T * firstCell = nullptr;
integer nrow = 0, ncol = 0;
integer rowStride = 0, colStride = 1;
constmatrixview ()
= default;
explicit constmatrixview (const T *const firstCell_, integer const nrow_, integer const ncol_, integer const rowStride_, integer const colStride_)
: firstCell (firstCell_), nrow (nrow_), ncol (ncol_), rowStride (rowStride_), colStride (colStride_) { }
constmatrixview (const constmatrix& other)
: constmatrixview (other.cells, other.nrow, other.ncol, other.ncol, 1_integer) { }
constmatrixview (const matrix& other) // shortcut the otherwise double conversion
: constmatrixview (other.cells, other.nrow, other.ncol, other.ncol, 1_integer) { }
constmatrixview (matrixview const& other)
: constmatrixview (other.firstCell, other.nrow, other.ncol, other.rowStride, other.colStride) { }
explicit constmatrixview (constvectorview const& vec, integer givenNrow, integer givenNcol) :
constmatrixview (vec.cells, givenNrow, givenNcol, givenNcol * vec.stride, vec.stride)
{
Melder_assert (givenNrow * givenNcol <= vec.size);
}
constvectorview operator[] (integer i) const {
return constvectorview (our firstCell + (i - 1) * our rowStride, our ncol, our colStride);
}
constvectorview row (integer rowNumber) const {
Melder_assert (rowNumber >= 1 && rowNumber <= our nrow);
return constvectorview (our firstCell + (rowNumber - 1) * our rowStride, our ncol, our colStride);
}
constvectorview column (integer columnNumber) const {
Melder_assert (columnNumber >= 1 && columnNumber <= our ncol);
return constvectorview (our firstCell + (columnNumber - 1) * our colStride, our nrow, our rowStride);
}
constvectorview diagonal () const {
return constvectorview (our firstCell, std::min (our nrow, our ncol), our rowStride + our colStride);
}
constmatrixview verticalBand (integer firstColumn, integer lastColumn) const {
const integer newNcol = lastColumn - (firstColumn - 1);
if (newNcol <= 0)
return constmatrixview ();
Melder_assert (firstColumn >= 1 && firstColumn <= our ncol);
Melder_assert (lastColumn >= 1 && lastColumn <= our ncol);
return constmatrixview (our firstCell + (firstColumn - 1) * our colStride,
our nrow, newNcol, our rowStride, our colStride);
}
constmatrixview part (integer firstRow, integer lastRow, integer firstColumn, integer lastColumn) const {
const integer newNrow = lastRow - (firstRow - 1), newNcol = lastColumn - (firstColumn - 1);
if (newNrow <= 0 || newNcol <= 0)
return constmatrixview ();
Melder_assert (firstRow >= 1 && firstRow <= our nrow);
Melder_assert (lastRow >= 1 && lastRow <= our nrow);
Melder_assert (firstColumn >= 1 && firstColumn <= our ncol);
Melder_assert (lastColumn >= 1 && lastColumn <= our ncol);
return constmatrixview (
our firstCell
+ (firstRow - 1) * our rowStride
+ (firstColumn - 1) * our colStride,
newNrow, newNcol,
our rowStride, our colStride
);
}
constmatrixview transpose () const {
return constmatrixview (our firstCell, our ncol, our nrow, our colStride, our rowStride);
}
};
/*
An automatrix is the sole owner of its payload, which is a matrix.
When the automatrix ends its life (goes out of scope),
it should destroy its payload (if it has not sold it),
because keeping a payload alive when the owner is dead
would continue to use some of the computer's resources (namely, memory).
*/
template
struct automatrix {
T *cells = nullptr;
integer nrow = 0, ncol = 0;
automatrix () // come into existence without a payload
= default;
explicit automatrix (integer givenNrow, integer givenNcol, MelderArray::kInitializationType initializationType) { // come into existence and manufacture a payload
Melder_assert (givenNrow >= 0);
Melder_assert (givenNcol >= 0);
our cells = MelderArray:: _alloc (givenNrow * givenNcol, initializationType);
our nrow = givenNrow;
our ncol = givenNcol;
}
automatrix (std::initializer_list > list) {
our nrow = uinteger_to_integer_a (list.size());
Melder_assert (our nrow > 0); // empty matrices should be created with automatrix() or automatrix (0, 10) or so
our ncol = uinteger_to_integer_a (list.begin()->size());
Melder_assert (our ncol > 0); // empty matrices should be created with automatrix() or automatrix (10, 0) or so
our cells = MelderArray:: _alloc (our nrow * our ncol, MelderArray::kInitializationType::RAW);
T *p = our cells;
for (auto row : list) {
const integer numberOfColumnsInThisRow = uinteger_to_integer_a (row.size());
Melder_assert (numberOfColumnsInThisRow == our ncol); // unfortunately, no support for static_assert here in C++17
for (auto cell : row)
* (p ++) = cell;
}
}
~automatrix () { // destroy the payload (if any)
if (our cells)
MelderArray:: _free (our cells, our nrow * our ncol);
}
matrix get() const { return matrix (our cells, our nrow, our ncol); } // let the public use the payload (they may change the values in the cells but not the cell pointer, nrow or ncol)
matrixview all () const {
return matrixview (our cells, our nrow, our ncol, our ncol, 1);
}
void adoptFromAmbiguousOwner (matrix const& given) { // buy the payload from a non-automatrix
our reset();
our cells = given.cells;
our nrow = given.nrow;
our ncol = given.ncol;
}
matrix releaseToAmbiguousOwner () { // sell the payload to a non-automatrix
T *oldCells = our cells;
our cells = nullptr; // disown ourselves, preventing automatic destruction of the payload
return matrix (oldCells, our nrow, our ncol);
}
/*
Disable copying via construction or assignment (which would violate unique ownership of the payload).
*/
automatrix (automatrix const& other) // disable copy constructor
= delete;
automatrix& operator= (automatrix const& other) // disable copy assignment
= delete;
/*
Enable moving of r-values (temporaries, implicitly) or l-values (for variables, via an explicit move()).
This implements buying a payload from another automatrix (which involves destroying our current payload).
*/
automatrix (automatrix&& other) noexcept : cells (other.cells), nrow (other.nrow), ncol (other.ncol) // enable move constructor
{
other.cells = nullptr; // disown source
other.nrow = 0; // to keep the source in a valid state
other.ncol = 0; // to keep the source in a valid state
}
automatrix& operator= (automatrix&& other) noexcept { // enable move assignment
if (other.cells != our cells) {
if (our cells)
MelderArray:: _free (our cells, our nrow * our ncol);
our cells = other.cells;
our nrow = other.nrow;
our ncol = other.ncol;
other.cells = nullptr; // disown source
other.nrow = 0; // to keep the source in a valid state
other.ncol = 0; // to keep the source in a valid state
} else if (! our cells) {
our nrow = other.nrow; // yes; if both cells are null, we still have to copy the shape, which could be { 0, 2 } or so
our ncol = other.ncol;
}
return *this;
}
vector operator[] (integer rowNumber) const {
return vector (our cells + (rowNumber - 1) * our ncol, our ncol);
}
vector row (integer rowNumber) const {
Melder_assert (rowNumber >= 1 && rowNumber <= our nrow);
Melder_assert (our cells || our ncol == 0);
return vector (our cells + (rowNumber - 1) * our ncol, our ncol);
}
vectorview column (integer columnNumber) const {
Melder_assert (columnNumber >= 1 && columnNumber <= our ncol);
return vectorview (our cells + (columnNumber - 1), our nrow, our ncol);
}
vectorview diagonal () const {
return vectorview (our cells, std::min (our nrow, our ncol), our ncol + 1);
}
matrixview horizontalBand (integer firstRow, integer lastRow) const {
const integer newNrow = lastRow - (firstRow - 1);
if (newNrow <= 0)
return matrixview ();
Melder_assert (firstRow >= 1 && firstRow <= our nrow);
Melder_assert (lastRow >= 1 && lastRow <= our nrow);
return matrixview (our cells + (firstRow - 1) * our ncol, newNrow, our ncol, our ncol, 1);
}
matrixview verticalBand (integer firstColumn, integer lastColumn) const {
const integer newNcol = lastColumn - (firstColumn - 1);
if (newNcol <= 0)
return matrixview ();
Melder_assert (firstColumn >= 1 && firstColumn <= our ncol);
Melder_assert (lastColumn >= 1 && lastColumn <= our ncol);
return matrixview (our cells + (firstColumn - 1), our nrow, newNcol, our ncol, 1);
}
matrixview part (integer firstRow, integer lastRow, integer firstColumn, integer lastColumn) const {
const integer newNrow = lastRow - (firstRow - 1), newNcol = lastColumn - (firstColumn - 1);
if (newNrow <= 0 || newNcol <= 0)
return matrixview ();
Melder_assert (firstRow >= 1 && firstRow <= our nrow);
Melder_assert (lastRow >= 1 && lastRow <= our nrow);
Melder_assert (firstColumn >= 1 && firstColumn <= our ncol);
Melder_assert (lastColumn >= 1 && lastColumn <= our ncol);
return matrixview (
our cells + (firstRow - 1) * our ncol + (firstColumn - 1),
newNrow, newNcol, our ncol, 1
);
}
matrixview transpose () const {
return matrixview (our cells, our ncol, our nrow, 1, our ncol);
}
void resize (integer newNrow, integer newNcol, MelderArray::kInitializationType initializationType = MelderArray::kInitializationType::ZERO) {
if (newNrow > our nrow || newNcol > our ncol) {
integer const newNumberOfCells = newNrow * newNcol;
T *newCells = MelderArray:: _alloc (newNumberOfCells, initializationType);
integer const numberOfRowsToCopy = std::min (our nrow, newNrow);
integer const numberOfColumnsToCopy = std::min (our ncol, newNcol);
for (integer irow = 1; irow <= numberOfRowsToCopy; irow ++)
for (integer icol = 1; icol <= numberOfColumnsToCopy; icol ++)
newCells [(irow - 1) * newNcol + (icol - 1)] = std::move (our cells [(irow - 1) * our ncol + (icol - 1)]);
if (our cells)
MelderArray:: _free (our cells, our nrow * our ncol);
our cells = newCells;
} else if (newNcol == our ncol) {
// do nothing
} else {
/*
The cells of the first new row already have the correct values.
*/
for (integer irow = 2; irow <= newNrow; irow ++)
for (integer icol = 1; icol <= newNcol; icol ++)
our cells [(irow - 1) * newNcol + (icol - 1)] = std::move (our cells [(irow - 1) * our ncol + (icol - 1)]);
}
our nrow = newNrow;
our ncol = newNcol;
}
void reset () noexcept { // on behalf of ambiguous owners (otherwise this could be in autoMAT)
if (our cells) {
MelderArray:: _free (our cells, our nrow * our ncol);
our cells = nullptr;
}
our nrow = 0;
our ncol = 0;
}
automatrix&& move () noexcept { return static_cast (*this); }
};
template
automatrix newmatrixraw (integer nrow, integer ncol) {
return automatrix (nrow, ncol, MelderArray::kInitializationType::RAW);
}
template
automatrix newmatrixzero (integer nrow, integer ncol) {
return automatrix (nrow, ncol, MelderArray::kInitializationType::ZERO);
}
template
void matrixcopy (matrixview const& target, constmatrixview const& source) {
Melder_assert (source.nrow == target.nrow && source.ncol == target.ncol);
for (integer irow = 1; irow <= source.nrow; irow ++)
for (integer icol = 1; icol <= source.ncol; icol ++)
target [irow] [icol] = source [irow] [icol];
}
template
void matrixcopy (matrixview const& target, matrixview const& source) {
matrixcopy (target, constmatrixview (source));
}
template
automatrix newmatrixcopy (constmatrixview const& source) {
automatrix result = newmatrixraw (source.nrow, source.ncol);
matrixcopy (result.all(), source);
return result;
}
template
automatrix newmatrixcopy (matrixview const& source) {
return newmatrixcopy (constmatrixview (source));
}
template
void assertCell (constvector const& x, integer elementNumber) {
Melder_assert (elementNumber >= 1 && elementNumber <= x.size);
}
template
void assertRow (constmatrix const& x, integer rowNumber) {
Melder_assert (rowNumber >= 1 && rowNumber <= x.nrow);
}
template
void assertColumn (constmatrix const& x, integer columnNumber) {
Melder_assert (columnNumber >= 1 && columnNumber <= x.ncol);
}
template
void assertCell (constmatrix const& x, integer rowNumber, integer columnNumber) {
assertRow (x, rowNumber);
assertColumn (x, columnNumber);
}
template
automatrix newmatrixpart (constmatrix const& x, integer firstRow, integer lastRow, integer firstColumn, integer lastColumn) {
assertCell (x, firstRow, firstColumn);
assertCell (x, lastRow, lastColumn);
integer numberOfRows = lastRow - (firstRow - 1);
Melder_assert (numberOfRows >= 0);
integer numberOfColumns = lastColumn - (firstColumn - 1);
Melder_assert (numberOfColumns >= 0);
automatrix result = newmatrixraw (numberOfRows, numberOfColumns);
for (integer irow = 1; irow <= numberOfRows; irow ++)
for (integer icol = 1; icol <= numberOfColumns; icol ++)
result [irow] [icol] = x [firstRow - 1 + irow] [firstColumn - 1 + icol];
return result;
}
template
automatrix newmatrixpart (matrix const& x, integer firstRow, integer lastRow, integer firstColumn, integer lastColumn) {
newmatrixpart (constmatrix (x), firstRow, lastRow, firstColumn, lastColumn);
}
template
struct autotensor3;
template
struct tensor3 {
T * cells = nullptr;
integer ndim1 = 0, ndim2 = 0, ndim3 = 0;
integer stride1 = 0, stride2 = 0, stride3 = 1;
tensor3 ()
= default;
tensor3 (autotensor3 const& other)
= delete;
explicit tensor3 (T * cells_,
integer ndim1_, integer ndim2_, integer ndim3_,
integer stride1_, integer stride2_, integer stride3_
) :
cells (cells_),
ndim1 (ndim1_), ndim2 (ndim2_), ndim3 (ndim3_),
stride1 (stride1_), stride2 (stride2_), stride3 (stride3_)
{ }
matrixview operator[] (integer dim1) const {
return matrixview (our cells + (dim1 - 1) * our stride1, our ndim2, our ndim3, our stride2, our stride3);
}
vectorview line1 (integer dim2, integer dim3) const {
Melder_assert (dim2 >= 1 && dim2 <= our ndim2);
Melder_assert (dim3 >= 1 && dim3 <= our ndim3);
return vectorview (
our cells
+ (dim2 - 1) * our stride2
+ (dim3 - 1) * our stride3,
our ndim1,
our stride1
);
}
vectorview line2 (integer dim1, integer dim3) const {
Melder_assert (dim1 >= 1 && dim1 <= our ndim1);
Melder_assert (dim3 >= 1 && dim3 <= our ndim3);
return vectorview (
our cells
+ (dim1 - 1) * our stride1
+ (dim3 - 1) * our stride3,
our ndim2,
our stride2
);
}
vectorview