Library that supports multidimensional Arrays

Click For Summary
SUMMARY

The discussion centers on the search for a C++ library that supports high-dimensional arrays and element-wise calculations similar to Python's NumPy. Participants clarify that native C++ can handle multidimensional arrays without the need for an additional library. They recommend using the Boost library for enhanced array functionalities and LAPACK for linear algebra operations involving multidimensional tensors. The consensus is that while such libraries exist, the learning curve may not justify their use compared to mastering native C++ multidimensional array capabilities.

PREREQUISITES
  • Understanding of C++ syntax and data structures
  • Familiarity with multidimensional arrays in C++
  • Basic knowledge of linear algebra concepts
  • Experience with libraries like Boost and LAPACK
NEXT STEPS
  • Research how to implement multidimensional arrays in C++
  • Explore the Boost library for advanced array functionalities
  • Learn about LAPACK for handling multidimensional tensors
  • Investigate performance optimization techniques for C++ numerical computations
USEFUL FOR

Software developers, particularly those transitioning from Python to C++, and anyone interested in optimizing numerical computations in C++ for scientific applications.

madsmh
Messages
32
Reaction score
2
I'm am looking for a C++ library that supports high-dimensional arrays ( 3=< ) and element-wise calculations.
Is there such a thing?
 
Technology news on Phys.org
What exactly are you trying to do? Native C++ supports as many dimensions as you want:

Code:
int fourDArray[10][10][10][10];
 
  • Like
Likes   Reactions: QuantumQuest
newjerseyrunner said:
What exactly are you trying to do? Native C++ supports as many dimensions as you want:

Code:
int fourDArray[10][10][10][10];

I have N x N 1x3 vectors that I need to be able to sum element-wise row-wise. I also need to do element-wise calculations on N x 3 arrays.
 
madsmh said:
I have N x N 1x3 vectors that I need to be able to sum element-wise row-wise.
This could be implemented as a three-dimensional array, something that is straightforward enough that I doubt that there is a library of the type you're looking for. With regard to "sum element-wise row-wise," you need to be a bit more explicit as to what you mean.
madsmh said:
I also need to do element-wise calculations on N x 3 arrays.
Nearly all C++ textbooks have a section on working with multidimension arrays. I'm sure there are also many online tutorials about the same subject.
 
Perhaps I wasn't clear. What I'm looking for is a numpy-like library for C++.
 
madsmh said:
Perhaps I wasn't clear. What I'm looking for is a numpy-like library for C++.
No, you were clear. What @newjerseyrunner and I are saying is that no such library is needed. Even if such a library existed, the time it would take to learn how to use that API would be at least as much as the time it takes to learn how to use multidimension arrays in C++.
 
Mark44 said:
No, you were clear. What @newjerseyrunner and I are saying is that no such library is needed. Even if such a library existed, the time it would take to learn how to use that API would be at least as much as the time it takes to learn how to use multidimension arrays in C++.

And that, like Mark44's post, does not answer my question.
 
Okay - the answer is the standard C++ (or C) library handles multidimensional arrays. The boost library has some extra add-ons for arrays. Multi-index containers, for example.

Lapack handles multidimensional tensors (matrices)- Linear algebra. More good add-ons.

http://www.boost.org/

http://www.netlib.org/lapack/

These extend the base standard library. This is as close to an answer for the question as posed. I believe:
Please tell us what you are trying to do. Please do not ask a question assuming you know how to accomplish the task, in this case some library you think must exist.
 
jim mcnamara said:
Okay - the answer is the standard C++ (or C) library handles multidimensional arrays.
As an example, here's some code that creates essentially a 4 x 4 matrix of column vectors, with each column vector being
##\begin {bmatrix} 1 \\ 2 \\ 3 \end{bmatrix}##

The following code creates a 3D array, one "slice" of which looks like this:
##\begin{bmatrix} 1 & 1 & 1 & 1 \\
2 & 2 & 2 & 2 \\
3 & 3 & 3 & 3 \end{bmatrix}##
The other "slices" look the same
C:
int main()
{
    int vectors[4][3][4];
    int i, j, k;

    for (k = 0; k < 4; k++) // Each column
    {
        for (j = 0; j < 3; j++) // Each row
        {
            for (i = 0; i < 4; i++) // Each slice
               vectors[i][j][k] = 1 + j;
        }
    }
 return 0;
}
 
  • Like
Likes   Reactions: QuantumQuest and jim mcnamara
  • #10
@madsmh, from one of your previous threads, you're coding in python. Python is a much higher-level language, so you might be unfamiliar with how things are done in a lower-level language like C or C++ where there isn't so much happening under the hood.
 
  • #11
There aren't many languages that fully support multi-dimensional arrays with built in operators. The only language I'm aware of that does this is APL (A Programming Language), where almost all of the built in operators support scalars, vectors, matrices, and arrays with 3 or more dimensions. I assume Matlab comes close to this, but I don't know Matlab. Prior posts already mentioned some libraries for C / C++.
 
  • #12
Thank you all for your suggestions. @Mark44 Is correct that I am used to Python with only a passing familiarity with C++.
The project I am working on is N-Body simulator for predicting Solar System orbits in Python, and I would like to speed
up the computations without loosing the graphical abilies of Python. As an example of what I would like to to do in C++ is the Verlet integrator which I have implemented like this:

Python:
def verlet(system, trajectory, rows, delta_t):

    delta_t2 = delta_t ** 2

    # TODO Implement Velocity Verlet
    for k in range(rows):
        if k == 0:
            # Get initial positions
            q0 = system.get_positions()

            # Save to trajectory
            trajectory.set_trajectory_position(q0)

        elif k == 1:
            # Get previous position
            q0 = trajectory.get_position_at_index(0)

            # Get initial velocity
            p0 = system.get_velocities()

            # Calculate accerleration
            a = system.get_accelerations()

            # Calculate q1
            q1 = q0 + p0 * delta_t + 0.5 * a * delta_t2

            # Save to trajectory
            trajectory.set_trajectory_position(q1)

            # Update positions of the planets
            system.set_positions(q1)

        # Calculate q_n+1
        else:
            # Calculate accerleration
            a = system.get_accelerations()

            # Get the prevous results
            qn1 = trajectory.get_position_at_index(k-2)
            qn = trajectory.get_position_at_index(k-1)

            # Calculate new new positions
            qplus = 2*qn - qn1 + a * delta_t2

            # Save to trajectory
            trajectory.set_trajectory_position(qplus)

            # Update positions of the planets
            system.set_positions(qplus)

As you can see I am able to do computations with multiple N x 3 arrays in a single line of code with Numpy.
I was hoping that there would be a similar facillities in C++ via a library.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K