NumCpp  2.14.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ReadMe.cpp

Examples from the Quick Start Guide in README.md at GitHub Repository

#include "NumCpp.hpp"
#include <filesystem>
#include <iostream>
int main()
{
// Containers
nc::NdArray<int> a0 = { { 1, 2 }, { 3, 4 } };
nc::NdArray<int> a1 = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
a1.reshape(2, 3);
auto a2 = a1.astype<double>();
// Initializers
auto a3 = nc::linspace<int>(1, 10, 5);
auto a4 = nc::arange<int>(3, 7);
auto a5 = nc::eye<int>(4);
auto a6 = nc::zeros<int>(3, 4);
auto a7 = nc::NdArray<int>(3, 4) = 0;
auto a8 = nc::ones<int>(3, 4);
auto a9 = nc::NdArray<int>(3, 4) = 1;
auto a10 = nc::nans(3, 4);
auto a12 = nc::empty<int>(3, 4);
auto a13 = nc::NdArray<int>(3, 4);
// Slicing/Broadcasting
auto a14 = nc::random::randInt<int>({ 10, 10 }, 0, 100);
[[maybe_unused]] auto value = a14(2, 3);
auto slice = a14({ 2, 5 }, { 2, 5 });
auto rowSlice = a14(a14.rSlice(), 7);
auto values = a14[a14 > 50];
a14.putMask(a14 > 50, 666);
// random
auto a15 = nc::random::randN<double>({ 3, 4 });
auto a16 = nc::random::randInt<int>({ 3, 4 }, 0, 10);
auto a17 = nc::random::rand<double>({ 3, 4 });
auto a18 = nc::random::choice(a17, 3);
// Concatenation
auto a = nc::random::randInt<int>({ 3, 4 }, 0, 10);
auto b = nc::random::randInt<int>({ 3, 4 }, 0, 10);
auto c = nc::random::randInt<int>({ 3, 4 }, 0, 10);
auto a19 = nc::stack({ a, b, c }, nc::Axis::ROW);
auto a20 = nc::vstack({ a, b, c });
auto a21 = nc::hstack({ a, b, c });
auto a22 = nc::append(a, b, nc::Axis::COL);
// Diagonal, Traingular, and Flip
auto d = nc::random::randInt<int>({ 5, 5 }, 0, 10);
auto a23 = nc::diagonal(d);
auto a24 = nc::triu(a);
auto a25 = nc::tril(a);
auto a26 = nc::flip(d, nc::Axis::ROW);
auto a27 = nc::flipud(d);
auto a28 = nc::fliplr(d);
// iteration
for (auto it = a.begin(); it < a.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << std::endl;
for (auto& arrayValue : a)
{
std::cout << arrayValue << " ";
}
std::cout << std::endl;
// Logical
auto a29 = nc::where(a > 5, a, b);
auto a30 = nc::any(a);
auto a31 = nc::all(a);
auto a32 = nc::logical_and(a, b);
auto a33 = nc::logical_or(a, b);
auto a34 = nc::isclose(a15, a17);
[[maybe_unused]] auto a35 = nc::allclose(a, b);
// Comparisons
auto a36 = nc::equal(a, b);
auto a37 = a == b;
auto a38 = nc::not_equal(a, b);
auto a39 = a != b;
#ifdef __cpp_structured_bindings
auto [rows, cols] = nc::nonzero(a);
#else
auto rowsCols = nc::nonzero(a);
auto& rows = rowsCols.first;
auto& cols = rowsCols.second;
#endif
// Minimum, Maximum, Sorting
auto value1 = nc::min(a);
auto value2 = nc::max(a);
auto value3 = nc::argmin(a);
auto value4 = nc::argmax(a);
auto a41 = nc::sort(a, nc::Axis::ROW);
auto a42 = nc::argsort(a, nc::Axis::COL);
auto a43 = nc::unique(a);
auto a44 = nc::setdiff1d(a, b);
auto a45 = nc::diff(a);
// Reducers
auto value5 = nc::sum<int>(a);
auto a46 = nc::sum<int>(a, nc::Axis::ROW);
auto value6 = nc::prod<int>(a);
auto a47 = nc::prod<int>(a, nc::Axis::ROW);
auto value7 = nc::mean(a);
auto a48 = nc::mean(a, nc::Axis::ROW);
auto value8 = nc::count_nonzero(a);
// I/O
a.print();
std::cout << a << std::endl;
auto tempDir = std::filesystem::temp_directory_path();
auto tempTxt = (tempDir / "temp.txt").string();
a.tofile(tempTxt, '\n');
auto a50 = nc::fromfile<int>(tempTxt, '\n');
auto tempBin = (tempDir / "temp.bin").string();
nc::dump(a, tempBin);
auto a51 = nc::load<int>(tempBin);
// Mathematical Functions
// Basic Functions
auto a52 = nc::abs(a);
auto a53 = nc::sign(a);
auto a54 = nc::remainder(a, b);
auto a55 = nc::clip(a, 3, 8);
auto xp = nc::linspace<double>(0.0, 2.0 * nc::constants::pi, 100);
auto fp = nc::sin(xp);
auto x = nc::linspace<double>(0.0, 2.0 * nc::constants::pi, 1000);
auto f = nc::interp(x, xp, fp);
// Exponential Functions
auto a56 = nc::exp(a);
auto a57 = nc::expm1(a);
auto a58 = nc::log(a);
auto a59 = nc::log1p(a);
// Power Functions
auto a60 = nc::power<int>(a, 4);
auto a61 = nc::sqrt(a);
auto a62 = nc::square(a);
auto a63 = nc::cbrt(a);
// Trigonometric Functions
auto a64 = nc::sin(a);
auto a65 = nc::cos(a);
auto a66 = nc::tan(a);
// Hyperbolic Functions
auto a67 = nc::sinh(a);
auto a68 = nc::cosh(a);
auto a69 = nc::tanh(a);
// Classification Functions
auto a70 = nc::isnan(a.astype<double>());
// nc::isinf(a);
// Linear Algebra
auto a71 = nc::norm<int>(a);
auto a72 = nc::dot<int>(a, b.transpose());
auto a73 = nc::random::randInt<int>({ 3, 3 }, 0, 10);
auto a74 = nc::random::randInt<int>({ 4, 3 }, 0, 10);
auto a75 = nc::random::randInt<int>({ 1, 4 }, 0, 10);
[[maybe_unused]] auto value9 = nc::linalg::det(a73);
auto a76 = nc::linalg::inv(a73);
auto a77 = nc::linalg::lstsq(a74, a75);
auto a78 = nc::linalg::matrix_power<int>(a73, 3);
auto a79 = nc::linalg::multi_dot<int>({ a, b.transpose(), c });
nc::linalg::svd(a.astype<double>(), u, s, vt);
return 0;
}
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:139
self_type & reshape(size_type inSize)
Definition NdArrayCore.hpp:4347
NdArray< dtypeOut > astype() const
Definition NdArrayCore.hpp:2257
constexpr double pi
Pi.
Definition Core/Constants.hpp:39
const double nan
NaN.
Definition Core/Constants.hpp:41
NdArray< double > inv(const NdArray< dtype > &inArray)
Definition inv.hpp:54
auto det(const NdArray< dtype > &inArray)
Definition det.hpp:131
NdArray< double > lstsq(const NdArray< dtype > &inA, const NdArray< dtype > &inB, double inTolerance=1e-12)
Definition lstsq.hpp:57
void svd(const NdArray< dtype > &inArray, NdArray< double > &outU, NdArray< double > &outS, NdArray< double > &outVt)
Definition svd.hpp:51
void seed(int inSeed)
Definition generator.hpp:46
dtype choice(const NdArray< dtype > &inArray)
Definition choice.hpp:102
NdArray< dtype > triu(uint32 inN, uint32 inM, int32 inOffset=0)
Definition tri.hpp:175
NdArray< bool > not_equal(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition not_equal.hpp:46
NdArray< dtype > min(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition min.hpp:44
double remainder(dtype inValue1, dtype inValue2) noexcept
Definition remainder.hpp:53
NdArray< dtype > tril(uint32 inN, int32 inOffset=0)
Definition tri.hpp:50
auto expm1(dtype inValue) noexcept
Definition expm1.hpp:48
double cbrt(dtype inValue) noexcept
Definition cbrt.hpp:48
NdArray< double > mean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition mean.hpp:52
constexpr dtype square(dtype inValue) noexcept
Definition square.hpp:47
std::pair< NdArray< uint32 >, NdArray< uint32 > > nonzero(const NdArray< dtype > &inArray)
Definition nonzero.hpp:48
NdArray< dtype > diagonal(const NdArray< dtype > &inArray, int32 inOffset=0, Axis inAxis=Axis::ROW)
Definition diagonal.hpp:47
NdArray< uint32 > argsort(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition argsort.hpp:46
NdArray< dtype > sort(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition sort.hpp:46
auto tanh(dtype inValue) noexcept
Definition tanh.hpp:49
auto log(dtype inValue) noexcept
Definition log.hpp:50
auto sin(dtype inValue) noexcept
Definition sin.hpp:49
NdArray< bool > logical_and(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition logical_and.hpp:50
int8 sign(dtype inValue) noexcept
Definition sign.hpp:52
auto tan(dtype inValue) noexcept
Definition tan.hpp:49
dtype clip(dtype inValue, dtype inMinValue, dtype inMaxValue)
Definition clip.hpp:50
auto log1p(dtype inValue) noexcept
Definition log1p.hpp:51
constexpr double interp(dtype inValue1, dtype inValue2, double inPercent) noexcept
Definition Functions/interp.hpp:48
NdArray< bool > logical_or(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition logical_or.hpp:50
NdArray< bool > any(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition any.hpp:46
NdArray< bool > equal(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition equal.hpp:45
auto abs(dtype inValue) noexcept
Definition abs.hpp:49
auto cos(dtype inValue) noexcept
Definition cos.hpp:49
auto sinh(dtype inValue) noexcept
Definition sinh.hpp:49
NdArray< dtype > where(const NdArray< bool > &inMask, const NdArray< dtype > &inA, const NdArray< dtype > &inB)
Definition where.hpp:52
NdArray< double > nans(uint32 inSquareSize)
Definition nans.hpp:45
NdArray< dtype > flipud(const NdArray< dtype > &inArray)
Definition flipud.hpp:46
NdArray< bool > isclose(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2, double inRtol=1e-05, double inAtol=1e-08)
Definition isclose.hpp:58
NdArray< dtype > unique(const NdArray< dtype > &inArray)
Definition unique.hpp:53
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition arange.hpp:59
auto sqrt(dtype inValue) noexcept
Definition sqrt.hpp:48
NdArray< dtype > diff(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition diff.hpp:52
NdArray< uint32 > argmax(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition argmax.hpp:46
NdArray< dtype > hstack(std::initializer_list< NdArray< dtype > > inArrayList)
Definition hstack.hpp:50
NdArray< bool > all(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition all.hpp:46
NdArray< uint32 > count_nonzero(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition count_nonzero.hpp:49
NdArray< dtype > append(const NdArray< dtype > &inArray, const NdArray< dtype > &inAppendValues, Axis inAxis=Axis::NONE)
Definition append.hpp:53
auto cosh(dtype inValue) noexcept
Definition cosh.hpp:49
NdArray< dtype > stack(std::initializer_list< NdArray< dtype > > inArrayList, Axis inAxis=Axis::NONE)
Definition stack.hpp:88
NdArray< dtype > setdiff1d(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition setdiff1d.hpp:54
bool isnan(dtype inValue) noexcept
Definition isnan.hpp:49
bool allclose(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2, double inTolerance=1e-5)
Definition allclose.hpp:54
NdArray< dtype > flip(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition flip.hpp:47
NdArray< uint32 > argmin(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition argmin.hpp:46
auto exp(dtype inValue) noexcept
Definition exp.hpp:49
NdArray< dtype > fliplr(const NdArray< dtype > &inArray)
Definition fliplr.hpp:46
NdArray< dtype > max(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition max.hpp:44
void dump(const NdArray< dtype > &inArray, const std::string &inFilename)
Definition dump.hpp:45
NdArray< dtype > vstack(std::initializer_list< NdArray< dtype > > inArrayList)
Definition vstack.hpp:49