What is the right ways to define a mathematical set in Mathematica?
I wrote the line
A = {1, 2, 3}
and
A := {1, 2, 3}
Both cases work, but I don't know which if any is right.
What is the right ways to define a mathematical set in Mathematica?
I wrote the line
A = {1, 2, 3}
and
A := {1, 2, 3}
Both cases work, but I don't know which if any is right.
a = {1, 2, 3} is the correct way.
a = {1,1} it contains duplicate elements.
– Kuba
Jul 25 '17 at 14:28
With Java 8 we got the cool new streams framework. In fact, one of the original designers of Java recommends to always use streams for all arrays / collections, unless you have specific reasons not to. Streams are the new default, especially together with collections.
Needs["JLink`"]
ReinstallJava[]
LoadJavaClass["java.util.Arrays","StaticsVisible"->True];
LoadJavaClass["java.util.stream.Collectors","StaticsVisible"->True];
t[s_]:=Arrays`asList[MakeJavaObject/@s];
list={1,1,2,3,4,5};
set=Arrays`stream[MakeJavaObject/@list]@collect[Collectors`toSet[]];
set@toArray[]
set@addAll[t@Range@20];
set@toArray[]
set@addAll[t@Array[4&,20]];
set@toArray[]
set@removeAll[t@Range[3,20,3]];
set@toArray[]
Look at all you have in that object:
set//Methods
You can even process streams in parallel (look, there are stream and parallelStream methods), so now you can use all your cores for stream processing -- 8, 12, 16, 24, 36 ... however many cores you may have.
{}is aListand the difference in:you can find in difference between Set (or =) and SetDelayed (or :=) – Kuba Jul 25 '17 at 09:52Unionwhich treat lists as if they were sets (they sort the lists and remove duplicate elements). You should check this page, choose a tutorial you find suitable for your level of knowledge, and work through at least the first few chapters. – Szabolcs Jul 25 '17 at 10:07