numxx::linspace
Creates an array of evenly spaced values within a given interval.
Syntax
NArray<double> linspace(const double start, const double stop, const uint32_t count, const bool endpoint = true);
Parameters
Returns
Time Complexity
O(n)
Examples
Create an NArray of 11 points from 0 to 1:
#include "NumXX.hpp"
int main() {
auto arr1 = numxx::linspace(0.0f, 1.0f, 11, true);
auto arr2 = numxx::linspace(0.0f, 1.0f, 11, false);
std::cout << arr1 << std::endl;
// >> [0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]
std::cout << arr2 << std::endl;
// >> [0. 0.0909091 0.1818182 0.2727273 0.3636364 0.4545455 0.5454546 0.6363636 0.7272727 0.8181819 0.9090909]
}
numxx::arange
Creates an array of evenly spaced values within a given interval.
Syntax
NArray<double> arange(const double start, const double stop, const double step = 1.0);
template <typename dtype = double, typename = std::enable_if_t<std::is_arithmetic_v<dtype>>>
NArray<dtype> arange(const dtype start, const dtype stop, const dtype step = dtype(1));
Parameters
Returns
Time Complexity
O(n)
Examples
Create NArrays of evenly spaced values using arange:
#include "NumXX.hpp"
int main() {
auto arr1 = numxx::arange(10, 1, -1);
auto arr2 = numxx::arange(0, 1.45, 0.2);
std::cout << arr1 << std::endl;
// >> [10 9 8 7 6 5 4 3 2]
std::cout << arr2 << std::endl;
// >> [0. 0.2 0.4 0.6 0.8 1. 1.2 1.4]
}
numxx::zeros
Creates an array of a given shape and calls the default constructor for all elements.
Syntax
template <typename dtype = double>
NArray<dtype> zeros(Shape shape);
template <typename dtype = double>
NArray<dtype> zeros(const size_t size);
Parameters
Returns
Time Complexity
O(n)
Examples
Initialise an NArray of arbitrary shape:
#include "NumXX.hpp"
int main() {
auto mat = numxx::zeros<float>({2, 3});
auto arr = numxx::zeros<float>(5);
std::cout << mat << std::endl;
// >> [[0 0 0]
// [0 0 0]]
std::cout << arr << std::endl;
// >> [0 0 0 0 0]
}
It also works for custom types:
#include "NumXX.hpp"
// Custom Type
struct MyType {
std::string str;
MyType() : str("default") {}
MyType(std::string s) : str(s) {}
// Needs a << print overload for NArray to print it
friend std::ostream &operator<<(std::ostream &os, const MyType &obj) {
os << obj.str;
return os;
}
};
int main() {
// Calls the default constructor of MyType
auto a = numxx::zeros<MyType>(3);
std::cout << a;
// >> [default default default]
}
numxx::zeros_like
Creates an NArray with the same dimensions / Shape as another NArray and calls the default constructor for all elements.
Syntax
template <typename dtype = double, typename U>
NArray<dtype> zeros_like(const NArray<U>& other);
template <typename dtype = double, typename U>
NArray<dtype> zeros_like(const std::vector<U>& other);
Parameters
Returns
Time Complexity
O(n)
Examples
#include "NumXX.hpp"
int main() {
auto arr = numxx::NArray({14, 3, 2, 56});
auto new_arr = numxx::zeros_like(arr);
auto vec = std::vector({10.0, 23.2, 1.3});
auto other_arr = numxx::zeros_like(vec);
std::cout << new_arr;
// >> [0 0 0 0]
std::cout << other_arr;
// >> [0 0 0]
}
Just like numxx::zeros, numxx::zeros_like also calls the default constructor for all elements of the output array:
#include "NumXX.hpp"
// Custom Type
struct MyType {
std::string str;
MyType() : str("default") {}
MyType(std::string s) : str(s) {}
// Needs a << print overload for NArray to print it
friend std::ostream &operator<<(std::ostream &os, const MyType &obj) {
os << obj.str;
return os;
}
};
int main() {
// Calls the default constructor of MyType
auto ref_arr = std::vector({1,1,2});
auto new_arr = numxx::zeros_like<MyType>(ref_arr);
auto ref_matrix = numxx::NArray({{1,1,2}, {3,4,0}});
auto new_matrix = numxx::zeros_like<MyType>(ref_matrix);
std::cout << new_arr;
// >> [default default default]
std::cout << new_matrix;
// >> [[default default default]
// [default default default]]
}
numxx::ones
Creates an array filled with ones.
Syntax
template <typename dtype = double, typename = std::enable_if_t<is_complex_or_arithmetic_v<dtype>>>
NArray<dtype> ones(const size_t size);
template <typename dtype = double, typename = std::enable_if_t<std::is_arithmetic_v<dtype>>>
NArray<dtype> ones(Shape shape);
Parameters
Returns
Time Complexity
O(n)
Examples
Create NArrays filled with ones:
#include "NumXX.hpp"
int main() {
auto mat = numxx::ones<float>({2, 3});
auto arr = numxx::ones<float>(5);
std::cout << mat << std::endl;
// >> [[1 1 1]
// [1 1 1]]
std::cout << arr << std::endl;
// >> [1 1 1 1 1]
}
numxx::ones_like
Creates an NArray of ones with the same shape as another NArray.
Syntax
template <typename dtype = double, typename U, typename = std::enable_if_t<std::is_arithmetic_v<dtype>>>
NArray<dtype> ones_like(const NArray<U>& other);
Parameters
Returns
Time Complexity
O(n)
Examples
Create an array of ones with the same shape as another array:
#include "NumXX.hpp"
int main() {
auto original = numxx::zeros<float>({3, 4});
auto like = numxx::ones_like(original);
std::cout << like << std::endl;
// >> [[1 1 1 1]
// [1 1 1 1]
// [1 1 1 1]]
}
numxx::eye
Creates a 2D identity matrix with ones on the diagonal and zeros elsewhere.
Syntax
template <typename dtype = double, typename = std::enable_if_t<is_complex_or_arithmetic_v<dtype>>>
NArray<dtype> eye(const size_t n, size_t m = 0);
Parameters
Returns
Time Complexity
O(n)
Examples
Create identity matrices:
#include "NumXX.hpp"
int main() {
auto I = numxx::eye<double>(3);
auto rect = numxx::eye<double>(2, 4);
std::cout << I << std::endl;
// >> [[1 0 0]
// [0 1 0]
// [0 0 1]]
std::cout << rect << std::endl;
// >> [[1 0 0 0]
// [0 1 0 0]]
}
numxx::identity
Creates a square identity matrix.
Syntax
template <typename dtype = double, typename = std::enable_if_t<std::is_arithmetic_v<dtype>>>
NArray<dtype> identity(const size_t n);
Parameters
Returns
Time Complexity
O(n)
Examples
Create a square identity matrix:
#include "NumXX.hpp"
int main() {
auto I = numxx::identity<float>(4);
std::cout << I << std::endl;
// >> [[1 0 0 0]
// [0 1 0 0]
// [0 0 1 0]
// [0 0 0 1]]
}
numxx::empty
Creates an uninitialised array with the given shape.
Syntax
template <typename dtype = double>
NArray<dtype> empty(const Shape& shape);
template <typename dtype = double>
NArray<dtype> empty(Shape&& shape);
template <typename dtype = double>
NArray<dtype> empty(const size_t size);
Parameters
Returns
Time Complexity
O(n)
Examples
Create an uninitialised NArray:
#include "NumXX.hpp"
int main() {
auto arr = numxx::empty<int>({2, 3});
// Uninitialised memory - contents are indeterminate
std::cout << arr.get_shape() << std::endl;
// >> [2 3]
}
numxx::empty_like
Creates an uninitialised NArray with the same shape as another NArray.
Syntax
template <typename dtype = double, typename U>
NArray<dtype> empty_like(const NArray<U>& other);
Parameters
Returns
Time Complexity
O(n)
Examples
Create an uninitialised NArray with the shape of another:
#include "NumXX.hpp"
int main() {
auto original = numxx::zeros<float>({3, 4});
auto like = numxx::empty_like<int>(original);
std::cout << like.get_shape() << std::endl;
// >> (2, 3)
}
numxx::full
Returns a new array of given shape and type, filled with fill_value.
Syntax
template <typename dtype = double>
NArray<dtype> full(Shape shape, dtype fill_value);
template <typename dtype = double>
NArray<dtype> full(Shape&& shape, dtype fill_value);
template <typename dtype = double>
NArray<dtype> full(const size_t shape, dtype fill_value);
Parameters
Returns
Time Complexity
O(n)
Examples
Create NArrays filled with a specific value:
#include "NumXX.hpp"
int main() {
auto arr = numxx::full<int>({2, 3}, 42);
auto vec = numxx::full<double>(5, 3.14);
std::cout << arr << std::endl;
// >> [[42 42 42]
// [42 42 42]]
std::cout << vec << std::endl;
// >> [3.14 3.14 3.14 3.14 3.14]
}
numxx::full_like
Returns a full array with the same shape as another array, filled with fill_value.
Syntax
template <typename dtype = double, typename U>
NArray<dtype> full_like(const NArray<U>& other, dtype fill_value);
Parameters
Returns
Time Complexity
O(n)
Examples
Create an array filled with a value, matching the shape of another array:
#include "NumXX.hpp"
int main() {
auto original = numxx::zeros<float>({2, 2});
auto like = numxx::full_like(original, 7.0);
std::cout << like << std::endl;
// >> [[7 7]
// [7 7]]
}
numxx::copy
Returns a shallow copy of the input array.
Syntax
template <typename dtype>
NArray<dtype> copy(const NArray<dtype>& arr);
Parameters
Returns
Time Complexity
O(n)
Examples
Create a shallow copy of an NArray:
#include "NumXX.hpp"
int main() {
auto arr = numxx::zeros<double>(5);
auto shallow = numxx::copy(arr);
// `shallow` shares data with `arr`, so they both share the same memory.
std::cout << arr.get_start_address() << std::endl;
// >> 0xa000003e0
std::cout << shallow.get_start_address() << std::endl;
// >> 0xa000003e0
}
numxx::deepcopy
Returns a deep copy of the input.
Syntax
template <typename dtype>
NArray<dtype> deepcopy(const NArray<dtype>& arr);
template <typename dtype>
dtype deepcopy(const dtype& elem);
Parameters
Returns
Time Complexity
O(n)
Examples
Create a deep copy of an NArray:
#include "NumXX.hpp"
int main() {
auto arr = numxx::zeros<double>(5);
auto deep = numxx::deepcopy(arr);
// `deep` now has independent memory to `arr`
std::cout << arr.get_start_address() << std::endl;
// >> 0xa000003e0
std::cout << deep.get_start_address() << std::endl;
// >> 0xa00000450
}
numxx::logspace
Creates an array of evenly spaced values on a logarithmic scale.
Syntax
NArray<double> logspace(const float start, const float stop, const uint32_t num = 50, const bool endpoint = true, const float base = 10.0f);
Parameters
Returns
Time Complexity
O(n)
Examples
Create an array of log-spaced values:
#include "NumXX.hpp"
int main() {
auto arr = numxx::logspace(0.0f, 2.0f, 5);
std::cout << arr << std::endl;
// >> [ 1. 3.16228 10. 31.62278 100. ]
}
numxx::geomspace
Creates an array of evenly spaced values on a geometric scale.
Syntax
NArray<double> geomspace(const float start, const float stop, const uint32_t num = 50, const bool endpoint = true);
Parameters
Returns
Time Complexity
O(n)
Examples
Create an array of geometrically spaced values:
#include "NumXX.hpp"
int main() {
auto up = numxx::geomspace(1.0f, 100.0f, 5);
auto down = numxx::geomspace(100.0f, 1.0f, 5);
std::cout << up << std::endl;
// >> [ 1. 3.16228 10. 31.62278 100. ]
std::cout << down << std::endl;
// >> [100. 31.62278 10. 3.16228 1. ]
}