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

Match for sparse array initialization #29

Closed
benkj opened this issue Jan 12, 2016 · 1 comment
Closed

Match for sparse array initialization #29

benkj opened this issue Jan 12, 2016 · 1 comment

Comments

@benkj
Copy link

benkj commented Jan 12, 2016

I've realized that Match.jl is perfect for creating in julia an equivalent of SparseArray
https://reference.wolfram.com/language/ref/SparseArray.html
which I find quite useful in Mathematica.

my basic implementation is this

macro sparsearray(size, rule)
    return quote
        _A = spzeros($size...)
        $(push!(rule.args, :(_ => 0)))

        for _itr in eachindex(_A)
            _A[_itr] = @match(_itr.I, $rule)
        end
        _A
    end
end

an example is

@sparsearray (5,5)  begin
        (n,m), if n==m+1 end => m
        (n,m), if n==m-1 end => n+10
        (1,5) => 1
end 

which creates the matrix
0.0 11.0 0.0 0.0 1.0
1.0 0.0 12.0 0.0 0.0
0.0 2.0 0.0 13.0 0.0
0.0 0.0 3.0 0.0 14.0
0.0 0.0 0.0 4.0 0.0

perhaps other people may find this application interesting, so it would be nice to include an example like this in the documentation

kmsquire added a commit that referenced this issue Jan 13, 2016
* @benkj's sparse array initialization
* VideoIO's Expr matching

(cherry picked from commit 197dccb)
(cherry picked from commit 2d42ebe)

See also #29
@kmsquire
Copy link
Contributor

Added to the docs--thanks!

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

No branches or pull requests

2 participants