Comments (8)
Hi. Can you clarify a bit here. What would be the corresponding Mathematica code?
from mathlink.jl.
Sorry, yes! I was playing with a toy Association and here is one example.
a=weval(W`chingAssociation = Association["team" -> "HOU", "lastName" -> "Ching", "firstName" -> "Brian", "position" -> "F", "baseSalary" -> Quantity[220000, "USDollars"], "guaranteedSalary" -> Quantity[220000, "USDollars"], "year" -> DateObject[{2007}, "Year", "Gregorian", -5.\`]]; c=chingAssociation[[{"baseSalary","position"}]]`)
Output
W"Association"(W"Rule"("baseSalary", W"Quantity"(220000, "USDollars")), W"Rule"("position", "F"))
Thanks!
from mathlink.jl.
sorry, I should have made it clearer. I would like the output (22000,"F") if that is possible.
thx!
from mathlink.jl.
Hi, the behavior you are describing is not the same as the matiamatica behaviour so it would be unwise to implement it.
In[1]:= chingAssociation=Association["team"->"HOU","lastName"->"Ching","firstName"->"Brian","position"->"F","baseSalary"->Quantity[220000,"USDollars"],"guaranteedSalary"->Quantity[220000,"USDollars"]]
Out[1]= <|team->HOU,lastName->Ching,firstName->Brian,position->F,baseSalary->$ 220000,guaranteedSalary->$ 220000|>
In[2]:= c=chingAssociation[[{"baseSalary","position"}]]
Out[2]= <|baseSalary->$ 220000,position->F|>
you can howerver acces individual elements directly:
In[3]:= c["baseSalary"]
Out[3]= $ 220000
In[4]:= {c["baseSalary"], c["position"]}
Out[4]= {Quantity[220000, "USDollars"], "F"}
In Julia and MathLink
this could be done by mapping the Association
to a Julia dictionary
function WAssociationToDict(Asso::MathLink.WExpr)
Wprint(Asso)
if Asso.head != W"Association"
error("Not an association")
end
D=Dict()
for Rule in Asso.args
if Rule.head != W"Rule"
error("not a rule")
end
if length(Rule.args) != 2
error("Bad rule")
end
D[Rule.args[1]]=Rule.args[2]
end
return D
end
Running the code could the look something like this:
julia> Ass = weval(W`Association["team" -> "HOU", "lastName" -> "Ching", "firstName" -> "Brian", "position" -> "F", "baseSalary" -> Quantity[220000, "USDollars"], "guaranteedSalary" -> Quantity[220000, "USDollars"], "year" -> DateObject[{2007}, "Year", "Gregorian", -5.]]`)
W"Association"(W"Rule"("team", "HOU"), W"Rule"("lastName", "Ching"), W"Rule"("firstName", "Brian"), W"Rule"("position", "F"), W"Rule"("baseSalary", W"Quantity"(220000, "USDollars")), W"Rule"("guaranteedSalary", W"Quantity"(220000, "USDollars")), W"Rule"("year", W"DateObject"(W"List"(2007), "Year", "Gregorian", -5.0)))
julia> D = WAssociationToDict(Ass)
Dict{Any, Any} with 7 entries:
"baseSalary" => W"Quantity"(220000, "USDollars")
"guaranteedSalary" => W"Quantity"(220000, "USDollars")
"year" => W"DateObject"(W"List"(2007), "Year", "Gregorian", -5.0)
"team" => "HOU"
"lastName" => "Ching"
"firstName" => "Brian"
"position" => "F"
julia> D["year"]
W"DateObject"(W"List"(2007), "Year", "Gregorian", -5.0)
julia> D["team"]
"HOU"
julia> [D[x] for x in ["year","team"]]
2-element Vector{Any}:
W"DateObject"(W"List"(2007), "Year", "Gregorian", -5.0)
"HOU"
@gganapat: Would a function like WAssociationToDict
be usefull?
from mathlink.jl.
from mathlink.jl.
Thanks to your tip, I learned that you can extract individual elements. So,
Ass.args[5].args[2].args[1] = 220000
It is now possible to extract arbitrary elements from the returned MathLink.WExpr object, which gives me a great sense of control! Thanks again!
Gani -
from mathlink.jl.
Hi @gganapat, I hope you will like this latest feature (W2Julia
)that i have sneaked into the 0.5.3 (https://github.com/JuliaInterop/MathLink.jl/releases/tag/v0.5.3) release from today. It aims to convert MathLink objects to Julia style objects.
For instance it can convert the "Assumptions" to a dictionary. Below are some examples:
@test W2Julia(W`{1,2,3}`) == [1,2,3]
@test W2Julia(W`{1,a,3}`) == [1,W"a",3]
@test W2Julia(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
@test W2Julia(W`Association["team" -> "HOU", "lastName" -> "Ching"]`) == Dict( "team" => "HOU" , "lastName" => "Ching")
@test W2Julia(W`Association["team" -> {1,2,3}, "lastName" -> "Ching"]`) == Dict( "team" => [1,2,3] , "lastName" => "Ching")
@test W2Julia(W`{1,Association["team" -> {1,2,3}, "lastName" -> "Ching"]}`) == [1,Dict( "team" => [1,2,3] , "lastName" => "Ching")]
from mathlink.jl.
I think this closes the original query.
The function W2Julia
is still quite limited, so if you find something that is missing (which is likely), please open a new ticket and I will fix it.
from mathlink.jl.
Related Issues (20)
- Including overloading of basic algebra HOT 10
- Bug: Julia Mathlink installation on Windows 10 HOT 4
- TagBot trigger issue HOT 9
- Possibility of allocation-free from julia via MathLink HOT 2
- Problem : UndefVarError: set_texOutput not defined HOT 2
- ParseError: KaTeX parse error HOT 2
- MathLink package can't find file "dep.jl" & fail to precompiling pkg. HOT 5
- Finding paths
- Wishlist from Wolfram HOT 2
- Symbolics to MathLink translation of expressions HOT 4
- Passing arrays to MathLink HOT 2
- MathLink with HypergeometricFunctions HOT 8
- W"List"(0.02) -> 0.02? HOT 1
- precision may be lost HOT 3
- MathLink not working on Julia 1.10? HOT 2
- Wrong path delimiter on Windows
- Mathematica arm64 version needed on M1 MacBook? HOT 8
- W2Julia expects an Expr, not a WExpr. HOT 6
- string() with weval-ed WExpr escape (\) some characters on arbitrary precision floating numbers with back quote HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mathlink.jl.