NumCpp: A Templatized Header Only C++ Implementation of the Python NumPy Library
Author: David Pilger [email protected]
Version: 1.3
License
Copyright 2020 David Pilger
Permission is hereby
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Testing
C++ Standards: C++11, C++14, and C++17 Compilers: VS 2017/2019, GCC 7.4.0/8.3/9.1, and Clang 6.0/8.0/9.0 Boost Versions: 1.68 and 1.70
This quick start guide is meant as a very brief overview of some of the things that can be done with NumCpp. For a full breakdown of everything available in the NumCpp library please visit the Full Documentation.
CONTAINERS
The main data structure in NumCpp is the NdArray. It is inherently a 2D array class, with 1D arrays being implemented as 1xN arrays. There is also a DataCube class that is provided as a convenience container for storing an array of 2D NdArrays, but it has limited usefulness past a simple container.
NumPy
NumCpp
a = np.array([[1, 2], [3, 4], [5, 6]])
nc::NdArray<int> a = { {1, 2}, {3, 4}, {5, 6} }
a.reshape([2, 3])
a.reshape(2, 3)
a.astype(np.double)
a.astype<double>()
INITIALIZERS
Many initializer functions are provided that return NdArrays for common needs.
NumPy
NumCpp
np.linspace(1, 10, 5)
nc::linspace<dtype>(1, 10, 5)
np.arange(3, 7)
nc::arange<dtype>(3, 7)
np.eye(4)
nc::eye<dtype>(4)
np.zeros([3, 4])
nc::zeros<dtype>(3, 4)
nc::NdArray<dtype>(3, 4) a = 0
np.ones([3, 4])
nc::ones<dtype>(3, 4)
nc::NdArray<dtype>(3, 4) a = 1
np.nans([3, 4])
nc::nans(3, 4)
nc::NdArray<double>(3, 4) a = nc::constants::nan
np.empty([3, 4])
nc::empty<dtype>(3, 4)
nc::NdArray<dtype>(3, 4) a
SLICING/BROADCASTING
NumCpp offers NumPy style slicing and broadcasting.
NumPy
NumCpp
a[2, 3]
a(2, 3)
a[2:5, 5:8]
a(nc::Slice(2, 5), nc::Slice(5, 8))
a({2, 5}, {5, 8})
a[:, 7]
a(a.rSlice(), 7)
a[a > 5]
a[a > 50]
a[a > 5] = 0
a.putMask(a > 50, 666)
RANDOM
The random module provides simple ways to create random arrays.
NumPy
NumCpp
np.random.seed(666)
nc::random::seed(666)
np.random.randn(3, 4)
nc::random::randn<double>(nc::Shape(3,4))
nc::random::randn<double>({3, 4})
np.random.randint(0, 10, [3, 4])
nc::random::randInt<int>(nc::Shape(3,4),0,10)
nc::random::randInt<int>({3, 4},0,10)
np.random.rand(3, 4)
nc::random::rand<double>(nc::Shape(3,4))
nc::random::rand<double>({3, 4})
np.random.choice(a, 3)
nc::random::choice(a, 3)
CONCATENATION
Many ways to concatenate NdArray are available.
NumPy
NumCpp
np.stack([a, b, c], axis=0)
nc::stack({a, b, c}, nc::Axis::ROW)
np.vstack([a, b, c])
nc::vstack({a, b, c})
np.hstack([a, b, c])
nc::hstack({a, b, c})
np.append(a, b, axis=1)
nc::append(a, b, nc::Axis::COL)
DIAGONAL, TRIANGULAR, AND FLIP
The following return new NdArrays.
NumPy
NumCpp
np.diagonal(a)
nc::diagonal(a)
np.triu(a)
nc::triu(a)
np.tril(a)
nc::tril(a)
np.flip(a, axis=0)
nc::flip(a, nc::Axis::ROW)
np.flipud(a)
nc::flipud(a)
np.fliplr(a)
nc::fliplr(a)
ITERATION
NumCpp follows the idioms of the C++ STL providing iterator pairs to iterate on arrays in different fashions.
NumPy
NumCpp
for value in a
for(auto it = a.begin(); it < a.end(); ++it)
for(auto& value : a)
LOGICAL
Logical FUNCTIONS in NumCpp behave the same as NumPy.
NumPy
NumCpp
np.where(a > 5, a, b)
nc::where(a > 5, a, b)
np.any(a)
nc::any(a)
np.all(a)
nc::all(a)
np.logical_and(a, b)
nc::logical_and(a, b)
np.logical_or(a, b)
nc::logical_or(a, b)
np.isclose(a, b)
nc::isclose(a, b)
np.allclose(a, b)
nc::allclose(a, b)
COMPARISONS
NumPy
NumCpp
np.equal(a, b)
nc::equal(a, b)
a == b
np.not_equal(a, b)
nc::not_equal(a, b)
a != b
rows, cols = np.nonzero(a)
auto [rows, cols] = nc::nonzero(a)
MINIMUM, MAXIMUM, SORTING
NumPy
NumCpp
np.min(a)
nc::min(a)
np.max(a)
nc::max(a)
np.argmin(a)
nc::argmin(a)
np.argmax(a)
nc::argmax(a)
np.sort(a, axis=0)
nc::sort(a, nc::Axis::ROW)
np.argsort(a, axis=1)
nc::argsort(a, nc::Axis::COL)
np.unique(a)
nc::unique(a)
np.setdiff1d(a, b)
nc::setdiff1d(a, b)
np.diff(a)
nc::diff(a)
REDUCERS
Reducers accumulate values of NdArrays along specified axes. When no axis is specified, values are accumulated along all axes.
NumPy
NumCpp
np.sum(a)
nc::sum(a)
np.sum(a, axis=0)
nc::sum(a, nc::Axis::ROW)
np.prod(a)
nc::prod(a)
np.prod(a, axis=0)
nc::prod(a, nc::Axis::ROW)
np.mean(a)
nc::mean(a)
np.mean(a, axis=0)
nc::mean(a, nc::Axis::ROW)
np.count_nonzero(a)
nc::count_nonzero(a)
np.count_nonzero(a, axis=0)
nc::count_nonzero(a, nc::Axis::ROW)
I/O
Print and file output methods. All NumCpp classes support a print() method and << stream operators.
NumPy
NumCpp
print(a)
a.print()
std::cout << a
a.tofile(filename, sep=’\n’)
a.tofile(filename, "\n")
np.fromfile(filename, sep=’\n’)
nc::fromfile<dtype>(filename, "\n")
np.dump(a, filename)
nc::dump(a, filename)
np.load(filename)
nc::load<dtype>(filename)
MATHEMATICAL FUNCTIONS
NumCpp universal functions are provided for a large set number of mathematical functions.
In
https://github.com/dpilger26/NumCpp/blob/master/include/NumCpp/NdArray/NdArrayCore.hpp
Line 4690, You've commented:
// NOTE: this needs to be defined outside of the class to get rid of a compiler
// error in Visual Studio
But I see it's already outside of the class, yet I get a Visual Studio compilation error:
Severity Code Description Project File Line Suppression State
Error(active) E0135 class template "nc::NdArray<dtype, Allocator>" has no member "nonzero" my_lib C : \NumCpp - master\include\NumCpp\NdArray\NdArrayCore.hpp 4693
What is needed to be done in order to fix this error ?
Here is my code, but the final executable program products the same number every time it starts. However, the following code can generate different numbers each time.
However, I have not found any API that can slice the array in Numcpp like this. It seems either Slice() or at() can not slice the array like this. Does it exist any API in Numcpp that can slice like this? Thanks!
I just installed Numcpp on a new machine and tried to compile a piece of code which was working on another machine.
However, on the new machine, I get compilation error: ambiguous overload for ‘operator+’ (operand types are ‘nc::NdArray<double>’ and ‘int’).
There are errors related to the ambiguity for nearly all the operators.
I was wondering if you could help me fix this issue.
I am using Boost 1.7, Clang 6.0, and tried different versions of gcc, including 7 , 8.3.0, 9.2.
Thanks in advance.
Hey,
I was wondering if you could let me know how I can get numpy.diag in Numcpp. I could not find anything regarding that method in the documentation. There is nc:diagonal, which is different with numpy.diag. Wondering if you have already implemented numpy.diag .
i try to build Numcpp on ubuntu1604. there are some bugs . cake is 3.15.2 and boost is 1.68.
***there is the log
Scanning dependencies of target Example
[ 50%] Building CXX object CMakeFiles/Example.dir/ReadMe.cpp.o
In file included from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Functions.hpp:57:0,
from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp.hpp:37,
from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/ReadMe.cpp:1:
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Functions/asarray.hpp: In function 'nc::NdArray nc::asarray(const std::array<_Tp, _Nm>&)':
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Functions/asarray.hpp:55:23: error: missing template arguments before '(' token
return NdArray(inArray);
^
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Functions/asarray.hpp: In function 'nc::NdArray nc::asarray(const std::vector<_RealType>&)':
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Functions/asarray.hpp:72:23: error: missing template arguments before '(' token
return NdArray(inVector);
^
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Functions/asarray.hpp: In function 'nc::NdArray nc::asarray(const std::deque<_Tp>&)':
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Functions/asarray.hpp:89:23: error: missing template arguments before '(' token
return NdArray(inDeque);
^
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Functions/asarray.hpp: In function 'nc::NdArray nc::asarray(const std::set&)':
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Functions/asarray.hpp:106:23: error: missing template arguments before '(' token
return NdArray(inSet);
^
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Functions/asarray.hpp: In function 'nc::NdArray nc::asarray(std::initializer_list<Tp>&)':
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Functions/asarray.hpp:124:23: error: missing template arguments before '(' token
return NdArray(inList);
^
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Functions/asarray.hpp: In function 'nc::NdArray nc::asarray(std::initializer_list<std::initializer_list<Tp> >&)':
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Functions/asarray.hpp:142:23: error: missing template arguments before '(' token
return NdArray(inList);
^
In file included from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations/DCM.hpp:35:0,
from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations.hpp:31,
from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp.hpp:44,
from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/ReadMe.cpp:1:
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations/Quaternion.hpp: At global scope:
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations/Quaternion.hpp:61:70: error: array must be initialized with a brace-enclosed initializer
std::array<double, 4> components = { 0.0, 0.0, 0.0, 1.0 };
^
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations/Quaternion.hpp:61:70: error: too many initializers for 'std::array<double, 4ul>'
In file included from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations/DCM.hpp:35:0,
from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations.hpp:31,
from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp.hpp:44,
from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/ReadMe.cpp:1:
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations/Quaternion.hpp: In static member function 'static nc::rotations::Quaternion nc::rotations::Quaternion::identity()':
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations/Quaternion.hpp:357:35: error: use of deleted function 'constexpr nc::rotations::Quaternion::Quaternion()'
return Quaternion();
^
In file included from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations/DCM.hpp:35:0,
from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations.hpp:31,
from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp.hpp:44,
from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/ReadMe.cpp:1:
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations/Quaternion.hpp:179:13: note: 'constexpr nc::rotations::Quaternion::Quaternion() noexcept' is implicitly deleted because its exception-specification does not match the implicit exception-specification 'noexcept (false)'
Quaternion() noexcept = default;
^
In file included from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations/DCM.hpp:35:0,
from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations.hpp:31,
from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp.hpp:44,
from /home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/ReadMe.cpp:1:
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations/Quaternion.hpp: In member function 'nc::NdArray nc::rotations::Quaternion::toNdArray() const':
/home/lisas/numcpp_new/numcpp_new/NumCpp/examples/ReadMe/../../include/NumCpp/Rotations/Quaternion.hpp:655:31: error: missing template arguments before '(' token
return NdArray(components);
^
CMakeFiles/Example.dir/build.make:62: recipe for target 'CMakeFiles/Example.dir/ReadMe.cpp.o' failed
make[2]: *** [CMakeFiles/Example.dir/ReadMe.cpp.o] Error 1
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/Example.dir/all' failed
make[1]: *** [CMakeFiles/Example.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Thanks for you project.
But I found that the broadcasting in numpy is likely not supported in NumCpp.
For example, I have two matrixs:
A=[1,2,3]
B = [[1], [2], [3]]
In numpy, A * B is
[ [1,2,3],
[2,4,6],
[3,6,9]]
However, in NumCpp, the error occured.
Thus I want to ask that are the shapes of two matrixs must be the same in NumCpp?
Describe the bug
Installed NumCpp latest on ArchLinux using CMake, and when building using g++ getting a lot of errors. Except for the include line, I haven't even used it anywhere inside my code.
To Reproduce
Steps to reproduce the behavior:
Install NumCpp using CMake
Include in a cpp file like this: #include <NumCpp.hpp>
GraphBLAST
GraphBLAST is a GPU implementation of GraphBLAS, an open standard for building blocks of graph algorithms. It gives data scientists without GPU programming experience the power to implement graph algorithms on the GPU. We
glam
A simple and fast 3D math library for games and graphics.
Development status
glam is in alpha stage. Minimal base functionality has been implemented and the look and feel of the API has solidified.
fpm
A C++ header-only fixed-point math library. "fpm" stands for "fixed-point math".
It is designed to serve as a drop-in replacement for floating-point types and aims to provide as much of the standard library's functionality a
linalg.h
linalg.h is a single header, public domain, short vector math library for C++. It is inspired by the syntax of popular shading and compute languages and is intended to serve as a lightweight alternative to projects
Introduction
nicemath is a compact single-header C++ library that provides data types and routines for basic linear algebra operations often encountered in computer graphics and game development.
To use the library, simply p
Realtime Math
This library is geared towards realtime applications that require their math to be as fast as possible. Much care was taken to maximize inlining opportunities and for code generation to be optimal when a
NumCpp: A Templatized Header Only C++ Implementation of the Python NumPy Library
Author: David Pilger [email protected]
Version: 1.3
License
Copyright 2020 David Pilger
Permission is hereby
ensmallen is a C++ header-only library for numerical optimization.
Documentation and downloads: http://ensmallen.org
ensmallen provides a simple set of abstractions for writing an objective function to optimize. It also prov
NOTE: The Fluent project has been renamed to Hydro, and the project has been broken up into multiple repositories. You can find all of them in the Hydro Project organization on Github. This repository has been archived and will be deleted e