dpilger26 / NumCpp

C++ implementation of the Python Numpy library

Home Page:https://dpilger26.github.io/NumCpp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Doubt] How are various dtypes handled in NumCpp

faze-geek opened this issue · comments

Hello everyone,
This is more of a doubt than an issue, I could not find a discussion forum or mailing list so I'm asking it here itself.
I am new to this interesting idea of implementing Numpy through CPP and had a very basic doubt.

How are the various dtypes of Numpy ndarray like int8, int16, int32, float32 handled in c++ ? Can someone point me to the implementation and briefly explain the thought behind it ?

Do you have a more specific question, I'm not certain what you are really asking?

The dtype is handled throughout the library as a template parameter, for example:

auto data = nc::NdArray<double>{5, 6};

This will create an NdArray of dtype double and dimensions 5x6.

Hi @dpilger26 . There are various dtypes we in numpy - https://numpy.org/doc/stable/reference/arrays.dtypes.html
I meant how do you smartly handle the dtype, ensure an annotation of right size and memory is used.
Like If I want to pick int32 (integer of 32 bits) or let's say something more advanced like complex128 (floating point complex number of 128 bits), what will be the cpp equivalent used.

If you can point me out to the files where the dtypes for NumCPP are designed it will be helpful as well.

I don't think you are understanding... it sounds like maybe you come from a Python background and haven't done a lot of C++? The dtypes aren't "designed" like you say. The dtype is a template parameter into NdArray<dtype>, and can be any C++ arithmetic type, as well as std::complex<T>, and can also act as an array for structured data types while disabling the arithmetic operations. The data is stored under the hood as a contiguous, row major block of memory of the chosen dtype. The NdArrayCore.hpp file is where this is all defined:

https://github.com/dpilger26/NumCpp/blob/master/include/NumCpp/NdArray/NdArrayCore.hpp

I got what I wanted. Thanks for answering my silly doubts :)

@dpilger26

The dtype is a template parameter into NdArray<dtype>, and can be any C++ arithmetic type, as well as std::complex<T>

Can the dtype be precision types like int8, int16, int32, int64 as well or all integers are just int?

I think for precision types C++ libraries like cstdint provide int8_t, int16_t and so on.

I see that is handled here. Thanks !

#include <cstdint>
namespace nc
{
//====================================Typedefs====================================
using int64 = std::int64_t;
using int32 = std::int32_t;
using int16 = std::int16_t;
using int8 = std::int8_t;
using uint64 = std::uint64_t;
using uint32 = std::uint32_t;
using uint16 = std::uint16_t;
using uint8 = std::uint8_t;
} // namespace nc