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

Add asymmetric operands to pointwise binary ops #290

Merged
merged 6 commits into from
Jul 5, 2021

Conversation

tchernobog
Copy link
Contributor

Alters the existing pointwise addition, subtraction, and multiplication binary operators so that they allow for different left-hand and right-hand side operands.

Note that in some cases this can lead to minor backwards incompatibilities due to the need to specify the type of one operand as it cannot be inferred automatically (e.g. when using an "eye" matrix). Please consider bumping the minor version number if merging.

Update functions in binops.rs to allow using different
types for left-hand side and right-hand side operators.

This is important when adding, multiplying, subtracting...
matrices and vectors of different types.
Move from requiring AddAssign to just Add, so that we
can drop the requirement for Clone.
Change from SubAssign to Sub as a bound for matrix subtraction.
Copy link
Collaborator

@mulimoen mulimoen left a comment

Choose a reason for hiding this comment

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

This looks good, and fits well in with the other generalisations made in this crate since the last release. It is not ideal to break type inference for existing code, although this is a minor cost for this additional functionality. We have already accumulated some breaking changes, so this is OK.

Could you add a couple of tests illuminating the added functionality? A couple of newtypes wrapping e.g. i16 + i32 = i64 would be nice.

@tchernobog
Copy link
Contributor Author

Done. Please note that the current incarnation only implements vector addition and subtraction. The other operations, and extending to also cover matrices, can be added if there is interest. I didn't need it right away for my own project, so I held off for now.

Comment on lines -354 to -358
let (oval, lr_elems) = items;
let binop_val = match lr_elems {
Left((_, val)) => binop(val, &N::zero()),
Right((_, val)) => binop(&N::zero(), val),
Both((_, lval, rval)) => binop(lval, rval),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Well spotted on this weird iteration

@mulimoen
Copy link
Collaborator

mulimoen commented Jul 5, 2021

Thanks for your great work on this @tchernobog!

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.

2 participants