-
-
Notifications
You must be signed in to change notification settings - Fork 96
Fix #682 #684
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
Merged
+6
−3
Merged
Fix #682 #684
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
|
|
||
| import ../tensor | ||
| import ./helpers/triangular | ||
| import std / [sequtils, bitops] | ||
| import std / [sequtils, bitops, strformat] | ||
|
|
||
| proc hilbert*(n: int, T: typedesc[SomeFloat]): Tensor[T] = | ||
| ## Generates an Hilbert matrix of shape [N, N] | ||
|
|
@@ -129,6 +129,7 @@ proc diagonal*[T](a: Tensor[T], k = 0, anti = false): Tensor[T] {.noInit.} = | |
| ## - anti: If true, get the k-th "anti-diagonal" instead of the k-th regular diagonal. | ||
| ## Result: | ||
| ## - A copy of the diagonal elements as a rank-1 tensor | ||
| bind `&` | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| assert a.rank == 2, "diagonal() only works on matrices" | ||
| assert k < a.shape[0], &"Diagonal index ({k=}) exceeds the output matrix height ({a.shape[0]})" | ||
| assert k < a.shape[1], &"Diagonal index ({k=}) exceeds the output matrix width ({a.shape[1]})" | ||
|
|
@@ -167,6 +168,7 @@ proc set_diagonal*[T](a: var Tensor[T], d: Tensor[T], k = 0, anti = false) = | |
| ## - k: The index k of the diagonal that will be changed. The default is 0 (i.e. the main diagonal). | ||
| ## Use k>0 for diagonals above the main diagonal, and k<0 for diagonals below the main diagonal. | ||
| ## - anti: If true, set the k-th "anti-diagonal" instead of the k-th regular diagonal. | ||
| bind `&` | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| assert a.rank == 2, "set_diagonal() only works on matrices" | ||
| assert d.rank == 1, "The diagonal passed to set_diagonal() must be a rank-1 tensor" | ||
| assert k < a.shape[0], &"Diagonal index ({k=}) exceeds input matrix height ({a.shape[0]})" | ||
|
|
@@ -259,6 +261,7 @@ proc tri*[T](shape: Metadata, k: static int = 0, upper: static bool = false): Te | |
| ## diagonal. The default is false. | ||
| ## Result: | ||
| ## - The constructed, rank-2 triangular tensor. | ||
| bind `&` | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| assert shape.len == 2, &"tri() requires a rank-2 shape as it's input but a shape of rank {shape.len} was passed" | ||
| assert k < shape[0], &"tri() received a diagonal index ({k=}) which exceeds the output matrix height ({shape[0]})" | ||
| assert k < shape[1], &"tri() received a diagonal index ({k=}) which exceeds the output matrix width ({shape[1]})" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid repeating
bind&`` in thediagonal, `set_diagonal`, and `tri` procedures, you can declare it once at the module level here. This improves maintainability by removing code duplication. The `bind` statement at the module level will ensure the `&` operator is available for all procedures in this file, including within generics.