2

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.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Gennaro Arguzzi
  • 959
  • 6
  • 16
  • 2
    {} is a List and the difference in : you can find in difference between Set (or =) and SetDelayed (or :=) – Kuba Jul 25 '17 at 09:52
  • 1
    There is no set data structure in Mathematica, only lists. There are functions such as Union which 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
  • https://www.wolfram.com/language/elementary-introduction/2nd-ed/ or https://www.wolfram.com/language/fast-introduction-for-math-students/en/ or https://www.wolfram.com/language/fast-introduction-for-programmers/en/ – Szabolcs Jul 25 '17 at 10:10

2 Answers2

2

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

Jack LaVigne
  • 14,462
  • 2
  • 25
  • 37
0

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.

Andreas Lauschke
  • 4,009
  • 22
  • 20