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 a = { {1, 2}, {3, 4}, {5, 6} }
a.reshape([2, 3])
a.reshape(2, 3)
a.astype(np.double)
a.astype()
INITIALIZERS
Many initializer functions are provided that return NdArrays for common needs.
NumPy
NumCpp
np.linspace(1, 10, 5)
nc::linspace(1, 10, 5)
np.arange(3, 7)
nc::arange(3, 7)
np.eye(4)
nc::eye(4)
np.zeros([3, 4])
nc::zeros(3, 4)
nc::NdArray(3, 4) a = 0
np.ones([3, 4])
nc::ones(3, 4)
nc::NdArray(3, 4) a = 1
np.nans([3, 4])
nc::nans(3, 4)
nc::NdArray(3, 4) a = nc::constants::nan
np.empty([3, 4])
nc::empty(3, 4)
nc::NdArray(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(nc::Shape(3,4))
nc::random::randN({3, 4})
np.random.randint(0, 10, [3, 4])
nc::random::randInt(nc::Shape(3,4),0,10)
nc::random::randInt({3, 4},0,10)
np.random.rand(3, 4)
nc::random::rand(nc::Shape(3,4))
nc::random::rand({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(filename, "\n")
np.dump(a, filename)
nc::dump(a, filename)
np.load(filename)
nc::load(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
i'm try to use vstack to stack a series ndarray,but the size of series is uncertain and the initializer_list can't convert from vector or list.
so,can i change the implement of the stack function?
Hi
I am trying to use NumCpp in my iOS project.
I have a cv::Mat object with double values in it. But I am unable to create NdArray from this source. It is only allowing me to use nc::uint8 as type.
auto ncArray = nc::NdArray(textSource.data, textSource.rows, textSource.cols);
What can I do to have NdArray with double type from cv::Mat object ? Kindly provide some guidance on it.
Describe the bug
I clone the whole directory into my project file, and I wrote include "NumCpp/include/NumCpp.hpp". Then error shows up. It says that 'NumCpp/Coordinates/Coordinate.hpp' file not found. So I delete that line of code, and I found out that every include statements in NumCpp.hpp are causing errors. But I am not sure what is happening.
Expected behavior
Include NumCpp.hpp file without any error or warning occurring.
Screenshots
Additional context
I am using vscode on arm64 m1 MacBook pro.
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>
I am going to Numpy library C++ API on Ubuntu and macOS.
how can i build NumCpp on Ubuntu and macOS?
If possible, please share how to build on various platform.
Hi I tried to install it on Ubuntu 20.04. Following error occured:
-- Compiling with C++ standard: 17
CMake Error at /usr/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:146 (message):
Could NOT find Boost (missing: Boost_INCLUDE_DIR) (Required is at least
version "1.68.0")
Call Stack (most recent call first):
/usr/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:393 (_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake-3.16/Modules/FindBoost.cmake:2179 (find_package_handle_standard_args)
CMakeLists.txt:47 (find_package)
-- Configuring incomplete, errors occurred!
I have done following steps:
git clone https://github.com/dpilger26/NumCpp.git
cd NumCpp
mkdir build
cd build
cmake ..
I did subtract operation between a vector1 with shape [2, 47] and vector2 with shape [1, 47]. The error message is:
Function: operator-
Line: 412
Error: Array dimensions do not match.libc++abi: terminating with uncaught exception of type std::invalid_argument: File: xxx/NumCpp/include/NumCpp/NdArray/NdArrayOperators.hpp
Function: operator-
Line: 412
Error: Array dimensions do not match.
It seems that NumCpp could not automatically transform vector2 into shape [2, 47]. How could I do this manually?
Is your feature request related to a problem? Please describe.
It would be great to have numpy.vsplit function in NumCpp, if I am not missing this in the current lib.
enhancement
opened by Ten000hours 0
Releases(Version_2.8.0)
Version_2.8.0(Jul 23, 2022)
Version 2.8.0
fixed error in inv when a zero was on the diagnol for Issue #132
fixed Issue #140
added bit_count, Computes the number of 1-bits in the absolute value of the input
added swapRows and swapCols methods to NdArray, and also free functions
Compile with NO_USE_BOOST definition to remove the Boost libraries as a dependency, with reduced functionality:
gcd with a pair of values (still available using a C++17 compliant compiler)
gcd array
lcm with a pair of values (still available using a C++17 compliant compiler)
lcm array
polynomial::chebyshev_t
polynomial::chebyshev_u
polynomial::hermite (still available using a C++17 compliant compiler)
polynomial::laguerre (still available using a C++17 compliant compiler)
polynomial::legendre_p (still available using a C++17 compliant compiler)
polynomial::legendre_q
polynomial::spherical_harmonic
random::beta
random::laplace
random::nonCentralChiSquared
random::triangle
random::uniformOnSphere
special::airy_ai
special::airy_ai_prime
special::airy_bi
special::airy_bi_prime
special::bernoulli
special::bessel_in (still available using a C++17 compliant compiler)
special::bessel_in_prime
special::bessel_jn (still available using a C++17 compliant compiler)
special::bessel_jn_prime
special::bessel_kn (still available using a C++17 compliant compiler)
special::bessel_kn_prime
special::bessel_yn (still available using a C++17 compliant compiler)
special::bessel_yn_prime
special::beta (still available using a C++17 compliant compiler)
special::cyclic_hankel_1
special::cyclic_hankel_2
special::digamma
special::erf
special::erf_inv
special::erfc
special::erfc_inv
special::gamma
special::gamma1pm1
special::log_gamma
special::polygamma
special::prime
special::riemann_zeta (still available using a C++17 compliant compiler)
special::spherical_bessel_jn (still available using a C++17 compliant compiler)
special::spherical_bessel_yn (still available using a C++17 compliant compiler)
special::spherical_hankel_1
special::spherical_hankel_2
special::trigamma
Added replace option into random::choice
Added nan_to_num function
Added complete and incomplete elliptical integrals of the first, second, and third kind to special namespace (requires either Boost or C++17 compliant compiler)
Added exponential integral to special namespace (requires either Boost or C++17 compliant compiler)
Added NO_MULTITHREAD compile definition to turn off algorithm multithreading from compliant compilers
Python wrapper for Kaldi's native I/O. The internal implementation uses C++ code from Kaldi. A Python wrapper with pybind11 is provided to read ark/scp files from Kaldi in Python.