#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 line3 (integer dim1, integer dim2) const { Melder_assert (dim1 >= 1 && dim1 <= our ndim1); Melder_assert (dim2 >= 1 && dim2 <= our ndim2); return vectorview ( our cells + (dim1 - 1) * our stride1 + (dim2 - 1) * our stride2, our ndim3, our stride3 ); } matrixview plane12 (integer dim3) const { Melder_assert (dim3 >= 1 && dim3 <= our ndim3); return matrixview ( our cells + (dim3 - 1) * our stride3, our ndim1, our ndim2, our stride1, our stride2 ); } matrixview plane13 (integer dim2) const { Melder_assert (dim2 >= 1 && dim2 <= our ndim2); return matrixview ( our cells + (dim2 - 1) * our stride2, our ndim1, our ndim3, our stride1, our stride3 ); } matrixview plane23 (integer dim1) const { Melder_assert (dim1 >= 1 && dim1 <= our ndim1); return matrixview ( our cells + (dim1 - 1) * our stride1, our ndim2, our ndim3, our stride2, our stride3 ); } tensor3 part ( integer firstDim1, integer lastDim1, integer firstDim2, integer lastDim2, integer firstDim3, integer lastDim3 ) const { const integer newNdim1 = lastDim1 - (firstDim1 - 1); const integer newNdim2 = lastDim2 - (firstDim2 - 1); const integer newNdim3 = lastDim3 - (firstDim3 - 1); if (newNdim1 <= 0 || newNdim2 <= 0 || newNdim3 <= 0) return tensor3 (); Melder_assert (firstDim1 >= 1 && firstDim1 <= our ndim1); Melder_assert (lastDim1 >= 1 && lastDim1 <= our ndim1); Melder_assert (firstDim2 >= 1 && firstDim2 <= our ndim2); Melder_assert (lastDim2 >= 1 && lastDim2 <= our ndim2); Melder_assert (firstDim3 >= 1 && firstDim3 <= our ndim3); Melder_assert (lastDim3 >= 1 && lastDim3 <= our ndim3); return tensor3 ( our cells + (firstDim1 - 1) * our stride1 + (firstDim2 - 1) * our stride2 + (firstDim3 - 1) * our stride3, newNdim1, newNdim2, newNdim3, our stride1, our stride2, our stride3 ); } }; template struct consttensor3 { const T * cells = nullptr; integer ndim1 = 0, ndim2 = 0, ndim3 = 0; integer stride1 = 0, stride2 = 0, stride3 = 1; consttensor3 () = default; consttensor3 (const autotensor3& other) = delete; explicit consttensor3 (const 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_) { } consttensor3 (tensor3 ten) : cells (ten.cells), ndim1 (ten.ndim1), ndim2 (ten.ndim2), ndim3 (ten.ndim3), stride1 (ten.stride1), stride2 (ten.stride2), stride3 (ten.stride3) { } constmatrixview operator[] (integer dim1) const { return constmatrixview (our cells + (dim1 - 1) * our stride1, our ndim2, our ndim3, our stride2, our stride3); } constvectorview line1 (integer dim2, integer dim3) const { Melder_assert (dim2 >= 1 && dim2 <= our ndim2); Melder_assert (dim3 >= 1 && dim3 <= our ndim3); return constvectorview ( our cells + (dim2 - 1) * our stride2 + (dim3 - 1) * our stride3, our ndim1, our stride1 ); } constvectorview line2 (integer dim1, integer dim3) const { Melder_assert (dim1 >= 1 && dim1 <= our ndim1); Melder_assert (dim3 >= 1 && dim3 <= our ndim3); return constvectorview ( our cells + (dim1 - 1) * our stride1 + (dim3 - 1) * our stride3, our ndim2, our stride2 ); } constvectorview line3 (integer dim1, integer dim2) const { Melder_assert (dim1 >= 1 && dim1 <= our ndim1); Melder_assert (dim2 >= 1 && dim2 <= our ndim2); return constvectorview ( our cells + (dim1 - 1) * our stride1 + (dim2 - 1) * our stride2, our ndim3, our stride3 ); } constmatrixview plane12 (integer dim3) const { Melder_assert (dim3 >= 1 && dim3 <= our ndim3); return constmatrixview ( our cells + (dim3 - 1) * our stride3, our ndim1, our ndim2, our stride1, our stride2 ); } constmatrixview plane13 (integer dim2) const { Melder_assert (dim2 >= 1 && dim2 <= our ndim2); return constmatrixview ( our cells + (dim2 - 1) * our stride2, our ndim1, our ndim3, our stride1, our stride3 ); } constmatrixview plane23 (integer dim1) const { Melder_assert (dim1 >= 1 && dim1 <= our ndim1); return constmatrixview ( our cells + (dim1 - 1) * our stride1, our ndim2, our ndim3, our stride2, our stride3 ); } consttensor3 part ( integer firstDim1, integer lastDim1, integer firstDim2, integer lastDim2, integer firstDim3, integer lastDim3 ) const { const integer newNdim1 = lastDim1 - (firstDim1 - 1); const integer newNdim2 = lastDim2 - (firstDim2 - 1); const integer newNdim3 = lastDim3 - (firstDim3 - 1); if (newNdim1 <= 0 || newNdim2 <= 0 || newNdim3 <= 0) return consttensor3 (); Melder_assert (firstDim1 >= 1 && firstDim1 <= our ndim1); Melder_assert (lastDim1 >= 1 && lastDim1 <= our ndim1); Melder_assert (firstDim2 >= 1 && firstDim2 <= our ndim2); Melder_assert (lastDim2 >= 1 && lastDim2 <= our ndim2); Melder_assert (firstDim3 >= 1 && firstDim3 <= our ndim3); Melder_assert (lastDim3 >= 1 && lastDim3 <= our ndim3); return consttensor3 ( our cells + (firstDim1 - 1) * our stride1 + (firstDim2 - 1) * our stride2 + (firstDim3 - 1) * our stride3, newNdim1, newNdim2, newNdim3, our stride1, our stride2, our stride3 ); } }; template struct autotensor3 : public tensor3 { autotensor3 () = default; // come into existence without a payload explicit autotensor3 (integer givenNdim1, integer givenNdim2, integer givenNdim3, MelderArray::kInitializationType initializationType) { // come into existence and manufacture a payload Melder_assert (givenNdim1 >= 0); Melder_assert (givenNdim2 >= 0); Melder_assert (givenNdim3 >= 0); our cells = MelderArray:: _alloc (givenNdim3 * givenNdim2 * givenNdim1, initializationType); our ndim1 = givenNdim1; our ndim2 = givenNdim2; our ndim3 = givenNdim3; our stride3 = 1; our stride2 = givenNdim3; our stride1 = givenNdim3 * givenNdim2; } ~autotensor3 () { // destroy the payload (if any) if (our cells) MelderArray:: _free (our cells, our ndim1 * our ndim2 * our ndim3); } //tensor3 get () { return { our at, our nrow, our ncol }; } // let the public use the payload (they may change the values in the cells but not the structure) const tensor3& get () const { return *this; } // let the public use the payload (they may change the values in the cells but not the structure) void adoptFromAmbiguousOwner (tensor3 const& given) { // buy the payload from a non-automatrix our reset(); our cells = given.cells; our ndim1 = given.ndim1; our ndim2 = given.ndim2; our ndim3 = given.ndim3; our stride1 = given.stride1; our stride2 = given.stride2; our stride3 = given.stride3; } tensor3 releaseToAmbiguousOwner () { // sell the payload to a non-automatrix T * oldCells = our cells; our cells = nullptr; // disown ourselves, preventing automatic destruction of the payload return tensor3 (oldCells, our ndim1, our ndim2, our ndim3, our stride1, our stride2, our stride3); } /* Disable copying via construction or assignment (which would violate unique ownership of the payload). */ autotensor3 (const autotensor3&) = delete; // disable copy constructor autotensor3& operator= (const autotensor3&) = 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 automatrix (which involves destroying our current payload). */ autotensor3 (autotensor3&& other) noexcept : tensor3 (other.get()) { // enable move constructor other.cells = nullptr; // disown source other.ndim1 = 0; // to keep the source in a valid state other.ndim2 = 0; // to keep the source in a valid state other.ndim3 = 0; // to keep the source in a valid state } autotensor3& operator= (autotensor3&& other) noexcept { // enable move assignment if (other.cells != our cells) { if (our cells) MelderArray:: _free (our cells, our ndim1 * our ndim2 * our ndim3); our cells = other.cells; our ndim1 = other.ndim1; our ndim2 = other.ndim2; our ndim3 = other.ndim3; our stride1 = other.stride1; our stride2 = other.stride2; our stride3 = other.stride3; other.cells = nullptr; // disown source other.ndim1 = 0; // to keep the source in a valid state other.ndim2 = 0; // to keep the source in a valid state other.ndim3 = 0; // to keep the source in a valid state } else if (! our cells) { our ndim1 = other.ndim1; // me and other may or may not be the same object our ndim2 = other.ndim2; our ndim3 = other.ndim3; } return *this; } void reset () noexcept { // on behalf of ambiguous owners (otherwise this could be in autoMAT) if (our cells) { MelderArray:: _free (our cells, our ndim1 * our ndim2 * our ndim3); our cells = nullptr; } our ndim1 = 0; our ndim2 = 0; our ndim3 = 0; } autotensor3&& move () noexcept { return static_cast (*this); } }; template autotensor3 newtensor3raw (integer ndim1, integer ndim2, integer ndim3) { return autotensor3 (ndim1, ndim2, ndim3, MelderArray::kInitializationType::RAW); } template autotensor3 newtensor3zero (integer ndim1, integer ndim2, integer ndim3) { return autotensor3 (ndim1, ndim2, ndim3, MelderArray::kInitializationType::ZERO); } template void tensor3copy (tensor3 const& target, consttensor3 const& source) { Melder_assert (source.ndim1 == target.ndim1 && source.ndim2 == target.ndim2 && source.ndim3 == target.ndim3); for (integer idim1 = 1; idim1 <= source.ndim1; idim1 ++) for (integer idim2 = 1; idim2 <= source.ndim2; idim2 ++) for (integer idim3 = 1; idim3 <= source.ndim3; idim3 ++) target [idim1] [idim2] [idim3] = source [idim1] [idim2] [idim3]; } template void tensor3copy (tensor3 const& target, tensor3 const& source) { tensor3copy (target, consttensor3 (source)); } template autotensor3 newtensor3copy (consttensor3 const& source) { autotensor3 result = newtensor3raw (source.ndim1, source.ndim2, source.ndim3); tensor3copy (result.get(), source); return result; } template autotensor3 newtensor3copy (tensor3 const& source) { return newtensor3copy (consttensor3 (source)); } template void assertDim1 (consttensor3 const& x, integer dim1) { Melder_assert (dim1 >= 1 && dim1 <= x.ndim1); } template void assertDim2 (consttensor3 const& x, integer dim2) { Melder_assert (dim2 >= 1 && dim2 <= x.ndim2); } template void assertDim3 (consttensor3 const& x, integer dim3) { Melder_assert (dim3 >= 1 && dim3 <= x.ndim3); } template void assertCell (consttensor3 const& x, integer dim1, integer dim2, integer dim3) { assertDim1 (x, dim1); assertDim2 (x, dim2); assertDim3 (x, dim3); } template autotensor3 newtensor3part (consttensor3 const& x, integer firstDim1, integer lastDim1, integer firstDim2, integer lastDim2, integer firstDim3, integer lastDim3 ) { assertCell (x, firstDim1, firstDim2, firstDim3); assertCell (x, lastDim1, lastDim2, lastDim3); integer ndim1 = lastDim1 - (firstDim1 - 1); Melder_assert (ndim1 >= 0); integer ndim2 = lastDim2 - (firstDim2 - 1); Melder_assert (ndim2 >= 0); integer ndim3 = lastDim3 - (firstDim3 - 1); Melder_assert (ndim3 >= 0); autotensor3 result = newtensor3raw (ndim1, ndim2, ndim3); for (integer idim1 = 1; idim1 <= ndim1; idim1 ++) for (integer idim2 = 1; idim2 <= ndim2; idim2 ++) for (integer idim3 = 1; idim3 <= ndim3; idim3 ++) result [idim1] [idim2] [idim3] = x [firstDim1 - 1 + idim1] [firstDim2 - 1 + idim2] [firstDim3 - 1 + idim3]; return result; } template autotensor3 newtensor3part (tensor3 const& x, integer firstDim1, integer lastDim1, integer firstDim2, integer lastDim2, integer firstDim3, integer lastDim3 ) { return newtensor3part (consttensor3 (x), firstDim1, lastDim1, firstDim2, lastDim2, firstDim3, lastDim3); } /* instead of vector we say VEC, because we want to have a one-to-one relation between VEC functions and the scripting language. For instance, we have raw_VEC and zero_VEC because Praat scripting has raw# and zero#. */ using VEC = vector ; using VECVU = vectorview ; using constVEC = constvector ; using constVECVU = constvectorview ; using autoVEC = autovector ; inline autoVEC raw_VEC (integer size) { return newvectorraw (size); } inline autoVEC zero_VEC (integer size) { return newvectorzero (size); } inline autoVEC copy_VEC (constVECVU const& source) { return newvectorcopy (source); } #define ARRAY_TO_VEC(doubleArray) constVEC (& doubleArray [0], sizeof (doubleArray) / sizeof (double)) /* And simply because we use vector so much as well, we have an abbreviation for that as well, namely INTVEC. But the scripting language has nothing that corresponds to INTVEC, so any numeric vector to be used by the scripting language should be a VEC, even if it contains integers. This is fine, as a double can contain an integer up to 54 bits. */ using INTVEC = vector ; using INTVECVU = vectorview ; using constINTVEC = constvector ; using constINTVECVU = constvectorview ; using autoINTVEC = autovector ; inline autoINTVEC raw_INTVEC (integer size) { return newvectorraw (size); } inline autoINTVEC zero_INTVEC (integer size) { return newvectorzero (size); } inline autoINTVEC copy_INTVEC (constINTVECVU const& source) { return newvectorcopy (source); } #define ARRAY_TO_INTVEC(integerArray) constINTVEC (& integerArray [0], sizeof (integerArray) / sizeof (integer)) using BOOLVEC = vector ; using BOOLVECVU = vectorview ; using constBOOLVEC = constvector ; using constBOOLVECVU = constvectorview ; using autoBOOLVEC = autovector ; inline autoBOOLVEC raw_BOOLVEC (integer size) { return newvectorraw (size); } inline autoBOOLVEC zero_BOOLVEC (integer size) { return newvectorzero (size); } inline autoBOOLVEC copy_BOOLVEC (constBOOLVECVU const& source) { return newvectorcopy (source); } #define ARRAY_TO_BOOLVEC(boolArray) constBOOLVEC (& boolArray [0], sizeof (boolArray) / sizeof (bool)) using BYTEVEC = vector ; using BYTEVECVU = vectorview ; using constBYTEVEC = constvector ; using constBYTEVECVU = constvectorview ; using autoBYTEVEC = autovector ; inline autoBYTEVEC raw_BYTEVEC (integer size) { return newvectorraw (size); } inline autoBYTEVEC zero_BYTEVEC (integer size) { return newvectorzero (size); } inline autoBYTEVEC copy_BYTEVEC (constBYTEVECVU const& source) { return newvectorcopy (source); } #define ARRAY_TO_BYTEVEC(byteArray) constBYTEVEC (& byteArray [0], sizeof (byteArray) / sizeof (byte)) using COMPVEC = vector ; using COMPVECVU = vectorview ; using constCOMPVEC = constvector ; using constCOMPVECVU = constvectorview ; using autoCOMPVEC = autovector ; inline autoCOMPVEC raw_COMPVEC (integer size) { return newvectorraw (size); } inline autoCOMPVEC zero_COMPVEC (integer size) { return newvectorzero (size); } inline autoCOMPVEC copy_COMPVEC (constCOMPVECVU const& source) { return newvectorcopy (source); } #define ARRAY_TO_COMPVEC(dcomplexArray) constCOMPVEC (& dcomplexArray [0], sizeof (dcomplexArray) / sizeof (dcomplex)) using MAT = matrix ; using MATVU = matrixview ; using constMAT = constmatrix ; using constMATVU = constmatrixview ; using autoMAT = automatrix ; inline autoMAT raw_MAT (integer nrow, integer ncol) { return newmatrixraw (nrow, ncol); } inline autoMAT zero_MAT (integer nrow, integer ncol) { return newmatrixzero (nrow, ncol); } inline autoMAT copy_MAT (constMATVU const& source) { return newmatrixcopy (source); } inline autoMAT part_MAT (constMAT const& source, integer firstRow, integer lastRow, integer firstColumn, integer lastColumn ) { return newmatrixpart (source, firstRow, lastRow, firstColumn, lastColumn); } using TEN3 = tensor3 ; using constTEN3 = consttensor3 ; using autoTEN3 = autotensor3 ; inline autoTEN3 raw_TEN3 (integer ndim1, integer ndim2, integer ndim3) { return newtensor3raw (ndim1, ndim2, ndim3); } inline autoTEN3 zero_TEN3 (integer ndim1, integer ndim2, integer ndim3) { return newtensor3zero (ndim1, ndim2, ndim3); } inline autoTEN3 copy_TEN3 (constTEN3 source) { return newtensor3copy (source); } inline autoTEN3 part_TEN3 (const constTEN3& source, integer firstDim1, integer lastDim1, integer firstDim2, integer lastDim2, integer firstDim3, integer lastDim3 ) { return newtensor3part (source, firstDim1, lastDim1, firstDim2, lastDim2, firstDim3, lastDim3); } using INTMAT = matrix ; using INTMATVU = matrixview ; using constINTMAT = constmatrix ; using constINTMATVU = constmatrixview ; using autoINTMAT = automatrix ; inline autoINTMAT raw_INTMAT (integer nrow, integer ncol) { return newmatrixraw (nrow, ncol); } inline autoINTMAT zero_INTMAT (integer nrow, integer ncol) { return newmatrixzero (nrow, ncol); } inline autoINTMAT copy_INTMAT (constINTMATVU source) { return newmatrixcopy (source); } using BOOLMAT = matrix ; using BOOLMATVU = matrixview ; using constBOOLMAT = constmatrix ; using constBOOLMATVU = constmatrixview ; using autoBOOLMAT = automatrix ; inline autoBOOLMAT raw_BOOLMAT (integer nrow, integer ncol) { return newmatrixraw (nrow, ncol); } inline autoBOOLMAT zero_BOOLMAT (integer nrow, integer ncol) { return newmatrixzero (nrow, ncol); } inline autoBOOLMAT copy_BOOLMAT (constBOOLMATVU source) { return newmatrixcopy (source); } using BYTEMAT = matrix ; using BYTEMATVU = matrixview ; using constBYTEMAT = constmatrix ; using constBYTEMATVU = constmatrixview ; using autoBYTEMAT = automatrix ; inline autoBYTEMAT raw_BYTEMAT (integer nrow, integer ncol) { return newmatrixraw (nrow, ncol); } inline autoBYTEMAT zero_BYTEMAT (integer nrow, integer ncol) { return newmatrixzero (nrow, ncol); } inline autoBYTEMAT copy_BYTEMAT (constBYTEMATVU source) { return newmatrixcopy (source); } conststring32 Melder_VEC (constVECVU const& value, bool horizontal = false); conststring32 Melder_fixed (constVECVU const& value, integer precision, bool horizontal = false); conststring32 Melder_MAT (constMATVU const& value); conststring32 Melder_fixed (constMATVU const& value, integer precision); inline void operator<<= (INTVECVU const& target, constINTVECVU const& source) { Melder_assert (target.size == source.size); for (integer i = 1; i <= target.size; i ++) target [i] = source [i]; } inline void operator<<= (BOOLVECVU const& target, constBOOLVECVU const& source) { Melder_assert (target.size == source.size); for (integer i = 1; i <= target.size; i ++) target [i] = source [i]; } inline void operator<<= (TEN3 const& target, constTEN3 const& source) { Melder_assert (target.ndim1 == source.ndim1); Melder_assert (target.ndim2 == source.ndim2); Melder_assert (target.ndim3 == source.ndim3); for (integer idim1 = 1; idim1 <= target.ndim1; idim1 ++) for (integer idim2 = 1; idim2 <= target.ndim2; idim2 ++) for (integer idim3 = 1; idim3 <= target.ndim3; idim3 ++) target [idim1] [idim2] [idim3] = source [idim1] [idim2] [idim3]; } /* End of file melder_tensor.h */ #endif