Skip to content

About

A simple C++ templated multiarray class for array computation

Resources

Code of conduct

Stars

2 stars

Watchers

1 watching

Forks

Latest commit

 

History

42 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Multiarray

multiarray is a C++ library that provides a powerful, multidimensional array structure with support for various operations, such as arithmetic, iteration, index manipulation, and more. It is designed to handle arrays with any number of dimensions and is optimized for performance while maintaining flexibility.

Features

  • Multidimensional arrays: Supports arrays with any number of dimensions, providing intuitive syntax for accessing and manipulating data.
  • Arithmetic operations: Perform common arithmetic operations directly on arrays, including element-wise and array-to-array operations.
  • Iterator support: Use iterators to traverse and manipulate arrays efficiently.
  • Subscript and linear index access: Access array elements using both subscript and linear indexing with automatic bounds checking.
  • Customization: Easily create custom matrix-like structures by extending the multiarray class.

Usage

Basic Example

#include "multiarray.hpp"

int main() {
  cx::multiarray<int, 2, 3> m = {1, 2, 3, 4, 5, 6};

  std::cout << m; // Output: 1 2 3 4 5 6
  
  int& min = m.min();
  int& max = m.max();
  
  std::cout << min; // Output: 1
  std::cout << max; // Output: 6
}

Multidimensional Arrays

multiarray can handle arrays with multiple dimensions:

cx::multiarray<int, M> m_1 = {};
cx::multiarray<int, M, N> m_2 = {};
cx::multiarray<int, M, N, O> m_3 = {};
cx::multiarray<int, M, N, O, P> m_4 = {};

You can access array elements using both linear index and subscript syntax:

m_1[i]
m_2[i]
m_3[i]

m_1(i)
m_2(i, j)
m_3(i, j, k)

Bounds checking can be performed with the at method:

m_1.at(i)
m_2.at(i, j)
m_3.at(i, j, k)

Arithmetic Operations

Perform arithmetic operations directly on multiarray objects:

cx::multiarray<int, 2, 3> m = {};

m *= value;
m /= value;
m %= value;
m += value;
m -= value;
m ^= value;
m |= value;
m &= value;
m <<= value;
m >>= value;

m * value;
m / value;
m % value;
m + value;
m - value;
m ^ value;
m | value;
m & value;
m << value;
m >> value;

m *= m;
m /= m;
m %= m;
m += m;
m -= m;
m ^= m;
m |= m;
m &= m;
m <<= m;
m >>= m;

m * m;
m / m;
m % m;
m + m;
m - m;
m ^ m;
m | m;
m & m;
m << m;
m >> m;

Iterator Support

Use iterators to traverse the array efficiently:

cx::multiarray<int, 2, 3> m = {};

m.begin();
m.end();
m.cbegin();
m.cend();
m.rbegin();
m.rend();
m.crbegin();
m.crend();

std::copy(m.begin(), m.end(), std::ostream_iterator<int>(std::cout, " "));

Array Swapping

Easily swap contents between two arrays:

cx::multiarray<std::string, 2, 3> m1 = {};
cx::multiarray<std::string, 2, 3> m2 = {};

m2.fill("X");
m1.swap(m2);

std::cout << m1; // Output: X X X X X X

Customization

You can create custom types based on multiarray by using templates:

#include <iostream>
#include "multiarray.hpp"

template <class T, std::size_t M, std::size_t N>
using matrix = cx::multiarray<T, M, N>;

int main() {
   matrix<int, 2, 3> m = {1, 2, 3, 4, 5, 6};
   
   for (std::size_t r = 0; r < 2; ++r) {
      for (std::size_t c = 0; c < 3; ++c) {
         std::size_t index = m.to_linear_index(r, c);
         std::cout << index << ' '; // Output: 0 1 2 3 4 5
      }
   }
}

You can also extend multiarray for more specialized structures:

#include <iostream>
#include "multiarray.hpp"

template <class T, std::size_t M, std::size_t N>
struct matrix : cx::multiarray<T, M, N> {
   constexpr std::size_t rsize() { return M; }
   constexpr std::size_t csize() { return N; }
};

int main() {
   matrix<int, 3, 2> m = {1, 2, 3, 4, 5, 6};
   
   for (std::size_t r = 0; r < m.rsize(); ++r) {
      for (std::size_t c = 0; c < m.csize(); ++c) {
         std::size_t index = m.to_linear_index(r, c);
         std::cout << index << ' '; // Output: 0 1 2 3 4 5
      }
   }
   
   puts("\n");
   
   for (std::size_t index = 0; index < m.rsize() * m.csize(); ++index) {
      auto subscript = m.to_subscript(index);
      std::size_t r = std::get<0>(subscript);
      std::size_t c = std::get<1>(subscript);
      std::cout << r << ' ' << c << std::endl;
   }
}

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

A simple C++ templated multiarray class for array computation

Resources

Code of conduct

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages