NumXX C++ Numerical Computing Library

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

start : double
The first number in the array.
stop : double
The upper bound of the array.
count : uint32_t
The number of elements in the array.
endpoint : bool (defaults to true)
Specifies whether or not the value of stop should be included as the last element of the array. Setting endpoint to true makes stop the last element of the array.

Returns

NArray<double>
An NArray object of type double with equally spaced elements starting at start and ending at stop. If endpoint is set to true, the elements are spaced such that stop is the final element of the array.

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

dtype : (defaults to double)
The data type of the elements in the array.
start : dtype
The lower bound of the interval. This value is included in the output array.
stop : dtype
The upper bound of the interval. This value is NOT included in the output array.
step : dtype
The spacing between values. For any output array, this is the distance between any two contiguous values.

Returns

NArray<dtype>
Returns an NArray of evenly spaced values.

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

dtype : (defaults to double)
The data type to be stored.
shape : Shape
Dimensions of the shape to create. (Creates an N-Dimensional array)
size : size_t
The size of the array (creates a 1D array).

Returns

NArray<dtype>
An NArray object of type dtype filled with zeros or the default constructed value of dtype.

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

dtype : (defaults to double)
The data type to be stored.
U :
The data type of the other NArray or std::vector. This can be any data type.
other : NArray, std::vector
The other NArray or vector to be used as a size reference for the new NArray.

Returns

NArray<dtype>
An NArray of the data type dtype's default value.

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

dtype : (defaults to double)
The data type to be stored. Must be arithmetic or complex (for the size_t overload) or arithmetic only (for the Shape overload).
size : size_t
The size of the array (creates a 1D array).
shape : Shape
Dimensions of the shape to create (creates an N-Dimensional array).

Returns

NArray<dtype>
An NArray object of type dtype filled with ones.

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

dtype : (defaults to double)
The data type of the output array. Must be arithmetic.
U :
The data type of the input NArray.
other : NArray
The NArray to use as a size reference for the new NArray.

Returns

NArray<dtype>
An NArray of the same shape as other, filled with ones.

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

dtype : (defaults to double)
The data type of the matrix. Must be arithmetic or complex.
n : size_t
Number of rows.
m : size_t (defaults to 0)
Number of columns. If 0 (default), m is set equal to n, creating a square matrix.

Returns

NArray<dtype>
A 2D NArray of shape (n, m) with ones on the diagonal and zeros elsewhere.

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

dtype : (defaults to double)
The data type of the matrix. Must be arithmetic.
n : size_t
The size of the square matrix (n x n).

Returns

NArray<dtype>
A square NArray of shape (n, n) with ones on the diagonal and zeros elsewhere.

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

dtype : (defaults to double)
The data type to be stored.
shape : Shape
Dimensions of the shape to create. (const& and && overloads)
size : size_t
The size of the array (creates a 1D array).

Returns

NArray<dtype>
An uninitialised NArray with the requested shape.

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

dtype : (defaults to double)
The data type of the output array.
U :
The data type of the input NArray.
other : NArray
The NArray to use as a size reference.

Returns

NArray<dtype>
An uninitialised NArray of the same shape as other.

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

dtype : (defaults to double)
The data type of the array.
shape : Shape
Dimensions of the shape to create.
fill_value : dtype
The value to fill the array with.

Returns

NArray<dtype>
An NArray of the given shape filled with fill_value.

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

dtype : (defaults to double)
The data type of the output array.
U :
The data type of the input NArray.
other : NArray
The NArray to use as a size reference.
fill_value : dtype
The value to fill the array with.

Returns

NArray<dtype>
An NArray of the same shape as other, filled with fill_value.

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

dtype :
The data type of the array.
arr : NArray
The array to copy.

Returns

NArray<dtype>
A shallow copy of arr (shares underlying data with the original).

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

dtype :
The data type of the input.
arr : NArray
The array to deep copy.
elem : dtype
Any element to deep copy.

Returns

NArray<dtype>
A deep copy of arr with its own independent data.
dtype
A copy of elem.

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

start : float
base raised to the power of start is the starting value of the sequence.
stop : float
base raised to the power of stop is the ending value of the sequence.
num : uint32_t (defaults to 50)
Number of samples to generate.
endpoint : bool (defaults to true)
If true, stop is the last sample. Otherwise, it is not included.
base : float (defaults to 10.0)
The base of the logarithm. Default is 10.0.

Returns

NArray<double>
An NArray of evenly spaced values on a logarithmic scale.

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

start : float
The starting value of the sequence.
stop : float
The ending value of the sequence.
num : uint32_t (defaults to 50)
Number of samples to generate.
endpoint : bool (defaults to true)
If true, stop is the last sample. Otherwise, it is not included.

Returns

NArray<double>
An NArray of geometrically spaced values.

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.     ]
}