Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math.Complex: genericize arithmetics with reals, replace mulbyi with comptime-generic i-shifted arithmetic #19202

Closed
wants to merge 5 commits into from

Conversation

expikr
Copy link
Contributor

@expikr expikr commented Mar 6, 2024

uses anytype to enable binary arithmetic operations (add/sub/mul/div) with real arguments

also added iadd, isub, imul, and idiv, and removed mulbyi which can be achieved with z.imul(1) that lowers to the equivalent data shuffle ops at comptime.

@expikr expikr changed the title math.Complex: implement arithmetic with scalars math.Complex: implement arithmetic with reals Mar 6, 2024
@expikr expikr changed the title math.Complex: implement arithmetic with reals math.Complex: support add/sub/mul/div with reals Mar 6, 2024
@expikr expikr changed the title math.Complex: support add/sub/mul/div with reals math.Complex: genericize arithmetics with reals, replace mulbyi with comptime-generic i-shifted arithmetic Jun 8, 2024
Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

provide alternate functions for operating on scalars rather than abusing anytype

@expikr
Copy link
Contributor Author

expikr commented Jun 13, 2024

provide alternate functions for operating on scalars rather than abusing anytype

Disagree. This is in preparation for #16278 according what this link says wrt C99 spec:

Standard arithmetic operators +, -, *, / can be used with real, complex, and imaginary types in any combination.

Until #16278 is implemented, the arithmetic methods serve as stand-ins for the arithmetic operators.

I can make them more strict with additional comptime type constraints, but the anytype argument is the entire premise.

c.c. @Snektron

@Snektron
Copy link
Collaborator

I feel that Andrew is right here. Maybe an alternative solution would be to add a constructor that initializes a Complex from just a real value? Together with #9938, this would form a relatively concise way to write complex operations on reals:

var a: Complex = .init(1, 2);
var b = a.mul(.real(3));
...

@expikr
Copy link
Contributor Author

expikr commented Jun 17, 2024

But this way they don't get lowered to scalar operations at compile time though?

The intent is to generalize permutation operations like mulbyi, divbyi, or neg into comptime-known algebraic expressions,
i.e. z.imul(1), z.imul(-1), z.mul(-1) which turns them into permutation ops without any extra arithmetic

And they are not just for operations with scalars. Consider expressions like $z + is$ where both z and are complex, being able to write z.iadd(s) is significantly more concise.

@expikr expikr closed this Jun 18, 2024
@expikr
Copy link
Contributor Author

expikr commented Jun 19, 2024

closed in favor of #20346

@expikr expikr deleted the patch-4 branch June 19, 2024 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants