r/learnmachinelearning • u/No-Minimum-7906 • 15d ago
Designing a C++ ML library from scratch — would you prefer A.transpose() or transpose(A) or A.T()?
I'm currently building a machine learning library from scratch in C++, and I'm thinking about the API design for tensor operations.
For a transpose operation, I'm considering these interfaces:
Option 1: Tensor B = A.transpose();
Option 2: Tensor B = transpose(A);
Option 3: Tensor B = A.T();
I'm curious about what other C++ developers would prefer when using a library.
Which syntax feels more natural and readable?
Thanks!
7
Upvotes
22
u/Ok-Library1020 15d ago
I'd lean toward A.transpose() returning a cheap strided view, with a free transpose(A) wrapper for generic code. A.T() reads nice for numpy folks, but a one-letter method name can feel a bit cryptic in C++.
6
u/personalityson 15d ago
Transpose(A, dim1, dim2) if ndim > 2
3
u/jmacey 15d ago
In my library I had
which transposed A but also you could do
just in case. This was for 3D graphics but similar principle.
3
u/GiggleSamurai 15d ago
For precedent: Eigen uses A.transpose(), Armadillo uses A.t() (plus a free trans(A)), and LibTorch uses A.t() and A.transpose(dim0, dim1), so a member function is what most C++ users will reach for first.
1
u/mystical-wizard 15d ago
Ideally both 1 and 2
1
u/Chippors 14d ago edited 14d ago
A.transpose() if it's a function that A implements, transpose<T>(A) if it's a generic algorithm that can operate on whatever T A is. I'd consider embodying its contract as a concept.
0
u/CREA-_-1111 15d ago
3