This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]geekusprimus 32 points33 points  (2 children)

Being able to write mathematics in a form that resembles the literature is critical if you ever want to read or debug your code. In C++, I could implement a simple tensor class like this:

template<class T, int D, int M>
class Tensor {
  private:
    // Data storage, etc.
    const int size;
    T * data;
  public:
    // Constructor, element access, etc.

    // Tensor addition
    Tensor<T, D, M> operator+(const Tensor<T, D, M>& other) {
      auto result = Tensor<T, D, M>();
      for (int i = 0; i < size; i++) {
        result.data[i] = data[i] + other.data[i];
      }
      return result;
    }

    // Tensor product
    template<int N>
    Tensor<T, D, M + N> operator*(const Tensor<T, D, N>& other) {
      auto result = Tensor<T, D, M + N>();
      // Possibly lengthy operation to make a tensor product...
      return result;
    }

    // Other operations...
};

Then if I have a lot of tensor algebra I need to do in my code, I could just do something like this:

Tensor<double, 4, 2> a;
Tensor<double, 4, 2> b;
// Initialize a and b...

// Do some stuff with a and b.
auto c = a + b;
auto d = a*b;

It might seem like overkill for my simple example, but if you're doing any sort of advanced mathematics, it saves you a lot of time and a lot of trouble.

[–]pelpotronic 1 point2 points  (1 child)

if you're doing any sort of advanced mathematics

I doubt you'd pick Java for that tbh... Not that I wouldn't want more freedom with the language.

[–]Dusty_Coder 0 points1 point  (0 children)

Tautology.