r/cpp_questions • u/No-Minimum-7906 • 23d ago
OPEN 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!
24
Upvotes
22
u/serendib 23d ago edited 22d ago
edit: removed my comment about numerical stability, see below
About your question: will you support chaining? If so then just have the function return a reference to itself
return *this;so you can do fun stuff like thisa.transpose().add(B).scale(0.5);7
u/DummyDDD 23d ago
I would prefer methods for in-place modifications, and free functions if the operators return copies, but it's not that important for unary operators like transpose.
6
u/Big-Rub9545 23d ago
I prefer option 2 the most. I personally find it weird (aesthetically moreso than for other reasons) that an explicit method call is returning an entirely new object. I usually expect method calls to be non-returning (while possibly modifying the object) or to return some field on the object.
Option 3 is introducing unnecessary confusion. ‘transpose’ is already very clear and short. The abbreviated ‘T’ also isn’t clear without a lot of contextual code around it (it’s not as clear in code as it is in math notation).
I would suggest also putting transpose as a static method or namespaced function. Of course static typing will prevent you from calling ‘transpose’ on any random object, but I think this still looks far clearer on intent:
Tensor B = Tensor::transpose(A);
Edit: typo.
4
u/Possibility_Antique 22d ago
In my linear algebra library, I support two syntaxes:
In the second, T is a tag that looks like this:
These two interfaces are equivalent, but the tag dispatch was fun/cute so I rolled with it.
4
u/JVApen 23d ago
How about
A | transpose?4
u/nunquam_rideo 23d ago edited 23d ago
2
u/mrtlo 23d ago
Really, memory semantics is more important than syntax for something like this IMHO... If it reads nicely, but performs like crap, who would use it? But why reinvent this wheel?
2
u/V15I0Nair 23d ago
You could use Option 1+2 like this
A.transpose() changes A, and returns A&. Then you can chain operations.
[[nodiscard]] static Matrix Transpose(const Matrix& A) creates a new object and will not change A.
2
u/plastic_eagle 20d ago
Return a transposed non-copyable view into your matrix from `Tensor::T`.
Have an implicit constructor from this view into a `Tensor` class, so that you can copy the output safely if necessary.
This can be a little dangerous, but only if you work hard to break things. It's no different from a unique_ptr being non-copyable, and nobody get upset about those.
Don't use free functions, they're annoying and don't pop up in autocomplete.
1
u/bayesianparoxism 23d ago
Not strongly opinionated,
Maybe more important is whether they return a view or a copy of the tensor
1
u/Gwinbar 23d ago
My expectation would be that the methods return a transposed view of the same object (no copying), while transpose(A) is the only one that might return a new object. I also agree that consistency is the most important thing.
1
u/tartaruga232 23d ago
auto B = A.transpose();1
u/Independent_Art_6676 23d ago
transpose is so common that you should overload an operator for it to keep the math concise and clean.
1
u/RishabhRD 22d ago
Depends, if the function doesn’t require any private data member, make it free function.
1
u/alfps 22d ago
So what does transpose mean for a general tensor, in your library?
E.g. for a 3D array there are 3! = 6 possible index permutations.
Which one do I invoke when I write
A.transpose()ortranspose(A)?1
u/lorsecco88 22d ago
It depends on your programming style. I'd avoid A.T() at all costs since it is not a meaningful method name.
1
u/davidc538 21d ago
I think option 1 reads the closest to the mathematical notation for this
1
u/wobey96 21d ago
I’d say B because the “data” and the “action on the data” are two separate things. For example you wouldn’t made Add() a function that belongs to the tensor. You’d either make it a stand along function Add(A, B) or overload the “+” operator. Maybe I’ve been watching too many game dev videos idk
1
u/etaithespeedcuber 21d ago
A.transpose() should transpose it inline, and A.transposed() should return a transposed tensor. Like sort and sorted in python
1
u/uraymeiviar 16d ago
depending on orientation, if u want speed transpose(A), for ease of use A.transpose()
0
u/papa_Fubini 23d ago
No one's gonna use it man, use what you want.
0
u/fortsnek274 23d ago
The one that works with Intellisense.
0
u/Pratik_Bodake 23d ago
Wth, matrices are used in ML?(Im FY, 😅)
0
u/[deleted] 23d ago
[deleted] — view deleted comment
3
u/[deleted] 23d ago
[removed] — view removed comment