Skip to content

Add Hyperbolic Trig and atan2#9

Merged
timschmidt merged 6 commits into
timschmidt:mainfrom
TimTheBig:hyperbolic-trig
May 26, 2026
Merged

Add Hyperbolic Trig and atan2#9
timschmidt merged 6 commits into
timschmidt:mainfrom
TimTheBig:hyperbolic-trig

Conversation

@TimTheBig
Copy link
Copy Markdown

This patch adds sinh, cosh, and tanh to mirror the inverses that are already in the code base. It also adds atan2

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request implements the two-argument arctangent function atan2 for both Computable and Real types, as well as the hyperbolic functions sinh, cosh, and tanh for Real. It also includes corresponding unit tests and micro-benchmarks. The review feedback highlights opportunities to optimize performance by removing redundant .clone() calls in Computable::atan2 and Real::tanh, which will prevent unnecessary heap allocations.

Comment thread src/computable/node.rs
}
_ => {}
}
let base = self.multiply(x.clone().inverse()).atan();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The parameter x is passed by value to atan2 and is not used after this line. Therefore, calling x.clone() before inverse() is redundant and incurs an unnecessary heap allocation of the Approximation enum. You can directly consume x by calling x.inverse().

        let base = self.multiply(x.inverse()).atan();

Comment thread src/real/arithmetic.rs
crate::trace_dispatch!("real", "tanh", "generic-exp-identity");
let positive = self.clone().exp()?;
let negative = self.neg().exp()?;
(positive.clone() - negative.clone()) / (positive + negative)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

In the generic path of tanh, positive and negative are cloned unnecessarily. Since Sub and Add are implemented for &Real and Real respectively, we can perform the subtraction by reference (&positive - &negative) and then consume the owned values in the addition (positive + negative). This avoids two expensive heap-allocating clones of Real.

        (&positive - &negative) / (positive + negative)

@timschmidt timschmidt merged commit 5c15f04 into timschmidt:main May 26, 2026
1 check passed
@TimTheBig
Copy link
Copy Markdown
Author

Thanks, I’ll make a follow up fixing the review comments

@timschmidt
Copy link
Copy Markdown
Owner

Pretty sure I already got to them

@TimTheBig
Copy link
Copy Markdown
Author

Perfect

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