2
$\begingroup$

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.

$\endgroup$
3
2
$\begingroup$

a = {1, 2, 3} is the correct way.

$\endgroup$
1
  • 2
    $\begingroup$ does not work for me, if I input a = {1,1} it contains duplicate elements. $\endgroup$
    – Kuba
    Jul 25 '17 at 14:28
0
$\begingroup$

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.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.