-
Notifications
You must be signed in to change notification settings - Fork 742
numty:0.1.0 #3329
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
Open
PabloRuizCuevas
wants to merge
1
commit into
typst:main
Choose a base branch
from
PabloRuizCuevas:patch-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
numty:0.1.0 #3329
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2024 Pablo | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
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 |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| ## Numty | ||
|
|
||
| ### Numeric Typst | ||
|
|
||
| A library for performing mathematical operations on n-dimensional matrices, vectors/arrays, and numbers in Typst, with support for broadcasting and handling NaN values. Numty’s broadcasting rules and API are inspired by NumPy. | ||
|
|
||
| ```typ | ||
| #import "numty.typ" as nt | ||
|
|
||
| // Define vectors and matrices | ||
| #let a = (1, 2, 3) | ||
| #let b = 2 | ||
| #let m = ((1, 2), (1, 3)) | ||
|
|
||
| // Element-wise operations with broadcasting | ||
| #nt.mult(a, b) // Multiply vector 'a' by scalar 'b': (2, 4, 6) | ||
| #nt.add(a, a) // Add two vectors: (2, 4, 6) | ||
| #nt.add(2, a) // Add scalar '2' to each element of vector 'a': (3, 4, 5) | ||
| #nt.add(m, 1) // Add scalar '1' to each element of matrix 'm': ((2, 3), (2, 4)) | ||
|
|
||
| // Dot product of vectors | ||
| #nt.dot(a, a) // Dot product of vector 'a' with itself: 14 | ||
|
|
||
| // Handling NaN cases in mathematical functions | ||
| #calc.sin((3, 4)) // Fails, as Typst does not support vector operations directly | ||
| #nt.sin((3.4)) // Sine of each element in vector: (0.14411, 0.90929) | ||
|
|
||
| // Generate equally spaced values and vectorized functions | ||
| #let x = nt.linspace(0, 10, 3) // Generate 3 equally spaced values between 0 and 10: (0, 5, 10) | ||
| #let y = nt.sin(x) // Apply sine function to each element: (0, -0.95, -0.54) | ||
|
|
||
| // Logical operations | ||
| #nt.eq(a, b) // Compare each element in 'a' to 'b': (false, true, false) | ||
| #nt.any(nt.eq(a, b)) // Check if any element in 'a' equals 'b': true | ||
| #nt.all(nt.eq(a, b)) // Check if all elements in 'a' equal 'b': false | ||
|
|
||
| // Handling special cases like division by zero | ||
| #nt.div((1, 3), (2, 0)) // Element-wise division, with NaN for division by zero: (0.5, float.nan) | ||
|
|
||
| // Matrix operations (element-wise) | ||
| #nt.add(m, 1) // Add scalar to matrix elements: ((2, 3), (2, 4)) | ||
|
|
||
| // matrix | ||
| #nt.transpose(m) // transposition | ||
| #nt.matmul(m,m) // matrix multipliation | ||
| #nt.matmul(c(1,2), r(2,3)) // colum vector times row vector multiplication. | ||
| #np.trace(m) // trace | ||
| #np.det(m) // nxn determinant | ||
|
|
||
| // printing | ||
| #nt.print(m, " != " , (1,2)) // long dollar print, see in pdf | ||
| #nt.p(m, " != " , (1,2)) // short long print print, see in pdf | ||
| ``` | ||
|
|
||
| ## Supported Features: | ||
|
|
||
| ### Dimensions: | ||
|
|
||
| Numty uses standard typst list as a base type, most 1d operations like dot are suported directly for them. | ||
|
|
||
| For matrix specific operations we use 2d arrays / nested arrays, that are also the standard typst list, but nested like in: ((1,2), (1,1)). | ||
|
|
||
| For convenience you can create column or row vectors with the #nt.c and #nt.r functions. | ||
|
|
||
| ```typ | ||
| #import "numty.typ" as nt | ||
| #import "numty.typ": c, r | ||
|
Comment on lines
+66
to
+67
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. Same here. |
||
|
|
||
| #let a = (1,2,3) | ||
| #let b = (3,2,1) | ||
| #c(..a) // ((1,),(2,),(3,)) | ||
| #r(..b) // ((3,2,1),) | ||
| #nt.matmul(c(..a), r(..b)) // column @ row | ||
|
|
||
| ``` | ||
|
|
||
| ### Logic Operations: | ||
| ```typ | ||
| #let a = (1,2,3) | ||
| #let b = 2 | ||
|
|
||
| #nt.eq(a,b) // (false, true, false) | ||
| #nt.all(nt.eq(a,b)) // false | ||
| #nt.any(nt.eq(a,b)) // true | ||
| ``` | ||
|
|
||
| ### Math operators: | ||
|
|
||
| All operators are element-wise, | ||
|
|
||
| ```typ | ||
| #nt.add((0,1,3), 1) // (1,2,4) | ||
| #nt.mult((1,3),(2,2)) // (2,6) | ||
| #nt.div((1,3), (2,0)) // (0.5,float.nan) | ||
| ``` | ||
|
|
||
| ### Algebra with Nan handling: | ||
|
|
||
| ```typ | ||
| #nt.log((0,1,3)) // (float.nan, 0 , 0.47...) | ||
| #nt.sin((1,3)) // (0.84.. , 0.14...) | ||
| ``` | ||
|
|
||
| ### Vector operations: | ||
|
|
||
| Basic vector operations | ||
|
|
||
| ```typ | ||
| #nt.dot((1,2),(2,4)) // 9 | ||
| #nt.normalize((1,4), l:1) // (1/5,4/5) | ||
| ``` | ||
|
|
||
| ### Others: | ||
|
|
||
| Functions for creating equally spaced indexes in linear and logspace, usefull for log plots, to sample in acordance to the logarithmic space. | ||
|
|
||
| ```typ | ||
| #nt.linspace(0,10,3) // (0,5,10) | ||
| #nt.logspace(1,3,3) | ||
| #nt.geomspace(1,3,3) | ||
| ``` | ||
|
|
||
| ### Matrix | ||
|
|
||
| ```typ | ||
| #nt.matmul(m,m) // matrix multiplication | ||
| #nt.det(((1,3), (3,4))) // nxn matrix determinant | ||
| #nt.trace(((1,3), (3,4))) // trace of square matrix | ||
| #nt.transpose(((1,3), (3,4))) // matrix transposition | ||
| ``` | ||
|
|
||
| ### Printing | ||
|
|
||
| Numty supports $ printing to the pdf of numerical matrices, both long and short format. | ||
|
|
||
| ```typ | ||
| #nt.print((1,2),(4,2))) // to display in the pdf | ||
| #nt.p((1,2),(4,2)), " random string ") // to display in the pdf | ||
| ``` | ||
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.
It would be better to show how to import the package from Universe, with
"@preview/numty:0.1.0".