-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
184 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,48 @@ | ||
# MaybeInplace | ||
|
||
[](https://github.com/avik-pal/MaybeInplace.jl/actions/workflows/CI.yml?query=branch%3Amain) | ||
[](https://github.com/JuliaTesting/Aqua.jl) | ||
[](https://julialang.zulipchat.com/#narrow/stream/279055-sciml-bridged) | ||
|
||
[](https://github.com/avik-pal/MaybeInplace.jl/actions/workflows/CI.yml) | ||
[](https://codecov.io/gh/avik-pal/MaybeInplace.jl) | ||
[](https://pkgs.genieframework.com?packages=MaybeInplace) | ||
[](https://github.com/JuliaTesting/Aqua.jl) | ||
|
||
[](https://github.com/SciML/ColPrac) | ||
[](https://github.com/SciML/SciMLStyle) | ||
|
||
MaybeInplace.jl does a simple thing: If you write a code for mutable arrays, that won't work for immutable arrays. This package provides a macro `@bb` or `@bangbang` that will automatically convert the code to make it work for immutable arrays as well. | ||
|
||
## Installation | ||
|
||
<p> | ||
MaybeInplace is a | ||
<a href="https://julialang.org"> | ||
<img src="https://raw.githubusercontent.com/JuliaLang/julia-logo-graphics/master/images/julia.ico" width="16em"> | ||
Julia Language | ||
</a> | ||
package. To install Expronicon, | ||
please <a href="https://docs.julialang.org/en/v1/manual/getting-started/">open | ||
Julia's interactive session (known as REPL)</a> and press <kbd>]</kbd> key in the REPL to use the package mode, then type the following command | ||
</p> | ||
|
||
```julia | ||
pkg> add MaybeInplace | ||
``` | ||
|
||
## How This Works? | ||
|
||
The code is simple enough to be self-explanatory. The basic idea is as follows, if you have the following code: | ||
|
||
```julia | ||
@bb @. x = y + z | ||
``` | ||
|
||
This macro will convert it to: | ||
|
||
```julia | ||
if setindex_trait(x) === CanSetindex() | ||
@. x = y + z | ||
else | ||
x = @. y + z | ||
end | ||
``` |
This file contains 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 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 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 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,90 @@ | ||
using MaybeInplace, StaticArrays, Test | ||
|
||
function copyto!!(y, x) | ||
@bb copyto!(y, x) | ||
return y | ||
end | ||
|
||
function eqop!!(y, x1, x2, x3, x4, x5) | ||
@bb y .= x1 | ||
@bb y .+= x2 | ||
@bb y .*= x3 | ||
@bb y .-= x4 | ||
@bb y ./= x5 | ||
return y | ||
end | ||
|
||
function dotmacro!!(y, x, z) | ||
@bb @. y = x * z | ||
return y | ||
end | ||
|
||
function matmul!!(y, x, z) | ||
@bb y = x × z | ||
return y | ||
end | ||
|
||
@testset "copyto!" begin | ||
x = [1.0, 1.0] | ||
y = [0.0, 0.0] | ||
@test copyto!!(y, x) == [1.0, 1.0] | ||
@test y == [1.0, 1.0] | ||
|
||
x = @SVector[1.0, 1.0] | ||
y = @SVector[0.0, 0.0] | ||
@test copyto!!(y, x) == @SVector[1.0, 1.0] | ||
@test y == @SVector[0.0, 0.0] | ||
|
||
x = @SMatrix[1.0 1.0; 1.0 1.0] | ||
y = @SMatrix[0.0 0.0; 0.0 0.0] | ||
@test copyto!!(y, x) == @SMatrix[1.0 1.0; 1.0 1.0] | ||
@test y == @SMatrix[0.0 0.0; 0.0 0.0] | ||
end | ||
|
||
@testset "(_/+/-/*/div)=" begin | ||
y = [0.0, 0.0] | ||
x1 = [1.0, 1.0] | ||
x2 = [1.0, 1.0] | ||
x3 = [1.0, 1.0] | ||
x4 = [1.0, 1.0] | ||
x5 = [1.0, 1.0] | ||
@test eqop!!(y, x1, x2, x3, x4, x5) == [1.0, 1.0] | ||
@test y == [1.0, 1.0] | ||
|
||
y = @SVector[0.0, 0.0] | ||
x1 = @SVector[1.0, 1.0] | ||
x2 = @SVector[1.0, 1.0] | ||
x3 = @SVector[1.0, 1.0] | ||
x4 = @SVector[1.0, 1.0] | ||
x5 = @SVector[1.0, 1.0] | ||
@test eqop!!(y, x1, x2, x3, x4, x5) == @SVector[1.0, 1.0] | ||
@test y == @SVector[0.0, 0.0] | ||
end | ||
|
||
@testset "dot" begin | ||
y = [0.0, 0.0] | ||
x = [1.0, 1.0] | ||
z = [1.0, 1.0] | ||
@test dotmacro!!(y, x, z) == [1.0, 1.0] | ||
@test y == [1.0, 1.0] | ||
|
||
y = @SVector[0.0, 0.0] | ||
x = @SVector[1.0, 1.0] | ||
z = @SVector[1.0, 1.0] | ||
@test dotmacro!!(y, x, z) == @SVector[1.0, 1.0] | ||
@test y == @SVector[0.0, 0.0] | ||
end | ||
|
||
@testset "matmul" begin | ||
y = [0.0, 0.0] | ||
x = [1.0 1.0; 1.0 1.0] | ||
z = [1.0, 1.0] | ||
@test matmul!!(y, x, z) == [2.0, 2.0] | ||
@test y == [2.0, 2.0] | ||
|
||
y = @SVector[0.0, 0.0] | ||
x = @SMatrix[1.0 1.0; 1.0 1.0] | ||
z = @SVector[1.0, 1.0] | ||
@test matmul!!(y, x, z) == @SVector[2.0, 2.0] | ||
@test y == @SVector[0.0, 0.0] | ||
end |
This file contains 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