dpilger26 / NumCpp

C++ implementation of the Python Numpy library
https://dpilger26.github.io/NumCpp
MIT License
3.59k stars 554 forks source link

[Doubt] How are various dtypes handled in NumCpp #209

Closed faze-geek closed 8 months ago

faze-geek commented 9 months ago

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 ?

dpilger26 commented 8 months ago

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.

faze-geek commented 8 months ago

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.

faze-geek commented 8 months ago

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

dpilger26 commented 8 months ago

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

faze-geek commented 8 months ago

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

faze-geek commented 8 months ago

@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?

faze-geek commented 8 months ago

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

faze-geek commented 8 months ago

I see that is handled here. Thanks ! https://github.com/dpilger26/NumCpp/blob/5e40aab74d14e257d65d3dc385c9ff9e2120c60e/include/NumCpp/Core/Types.hpp#L30-L43