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