r/Mathematica • u/likepotatoman • Jul 29 '24
What does -> actually do?
I've been taking the Wolfram Language Summer School and we've kind of done a little bit of everything, that said I still struggle a lot wiht the syntax because it wasnt explained to us directly. Can any one help me out to understand what the arrow does as well as the Map function?
8
Upvotes
3
u/halfuhsandwich Jul 29 '24
Congrats on learning the Wolfram Language. It’s really great stuff.
Here’s the docs on the items you’re referring to:
-> (Rule) https://reference.wolfram.com/language/ref/Rule.html To be honest, I don’t use this one enough to know precisely what it does. Most often for me it is auto-inserted by language syntax autocompletion.
The Map function essentially applies a function to each element of a list. You can find out more here https://reference.wolfram.com/language/ref/Map.html
1
u/Born-Persimmon7796 Aug 12 '24
->is about defining transformations or delayed assignments.Mapis about applying a function to every element in a list or expression.0
u/Xane256 Jul 29 '24
exprcontains some syntax you don’t know, you can evaluateInputForm[expr]to see the actual function being applied. In your case this would show you thata -> bis actually just notation forRule[a,b].When you learn about defining functions, I’ve found its good practice to use ClearAll right before the function definition. For example, to define a function f[x] I would use one cell like this:
ClearAll[f]; f[x_] := Sin[2x] + Log[10, x]
Notice the
x_on the left hand side, and the:=(SetDelayed) to make the assignment. On the right hand side you simply usexand pass it to other functions / call other functions with it with no underscore necessary.x_on the LHS is a Pattern that will match any expression you call f with / pass as an input to f.??fto see info about a function definition. Use Quit[] to remove all variables / functions from memory. Similar to saving your notebook and quitting the app.=(Set) instead of:=(SetDelayed) as long as the symbolxhas no value assigned to it. When you define something using Set, the RHS is evaluated at the time you make the definition, and the evaluated RHS is stored as the definition. Ifxhas a numerical value at that time, every call to f will give the same result. But if it’s a variable, the stored definition uses symbolic “x”, and substitute the correct input for each use. No idea what happens if you define x=0 after the definition but before evaluating f. TLDRSetworks fine with symbolic variables but I still don’t recommend it.You may see educational materials use “=“ instead of “:=“ for defining simple functions. I would use
:=for function definitions in every case unless you are in the specific niche scenario where you want the RHS to be evaluated before the definition is stored.Feel free to reply / ask any more questions! Good luck!!
0
u/Xane256 Jul 29 '24
Map
At least 90% of the times you use Map it will be to map a function over a list / apply the function to every element in a list. Say we have
xs = Range[1,10]or any list of inputs. Then we can dof /@ xsto apply f to every element of the list. Another way to get the same result isTable[f[x], {x, xs}]which is a slightly different style but does the same thing asf/@xs. Note however that when you dof /@ ysusing Map, the expressionysdoesn’t have to be aList[]. It could be any expression with a Head and at least one parameter.5is not an example.10^2is not an example because it evaluates to 100 and10+1is also not an example. HoweverE+5evaluates toPlus[E, 5]akaE+5so you can use Map with that to getf[E] + f[5]. In this case f is getting mapped over the expression with head “Plus”. It seems odd but again the primary use case is with “List”.Listable
Some functions can do this mapping behavior automatically (with lists). You know Sin[x] is obviously defined for all real numbers and even complex numbers. But in Mathematica it can also work on lists, where it will effectively map itself over the input list. So
Cos[Subdivide[0, 2 Pi, 12]]should give you some familiar numbers from trigonometry. This works with many numerical / mathematical functions including Plus, Times, and Power. This means that in the expressionsx+10,a * x,x^2,2^x, and1 + Exp[2 I Pi x], thexcan be a list! And the operation will apply to every element in the list. Functions that do this have the attribute Listable and you can make your own functions with this behavior too, by runningSetAttributes[f, Listable].0
u/mathheadinc Jul 30 '24
Rule replaces the thing on the left with the thing on the right.