NumXX C++ Numerical Computing Library

numxx::linspace

Creates an array of evenly spaced values within a given interval.

Syntax

NArray<double> linspace(const float start, const float stop, const size_t count, const bool endpoint = true);

Parameters

start : double
The first number in the array.
stop : double
The upper bound of the array.
count : size_t
The number of elements in the array.
endpoint : bool (defaults to true)
Spcifies whether on 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

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)
None
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, typename = std::enable_if_t<std::is_arithmetic_v<dtype>>>
NArray<dtype> zeros(Shape shape);
template <typename dtype = double, typename = std::enable_if_t<std::is_arithmetic_v<dtype>>>
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<double>
An NArray object of type dtype filled with zeros or the default constructed value of dtype.
int
description and what not.

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