# Tag Info

## Hot answers tagged syntax

19

I know this has been answered already on this site, but I cannot seem to find it. Map and Apply do subtly different things. For example, Map[f, {a,b,c}] (* {f[a], f[b], f[c]} *) If you have a list that is more deeply nested, without using the third argument which is for level specification, you get Map[f, {{a,b}, {c}}] (* {f[{a,b}], f[{c}]} *) or, if ...

19

One way would be to redirect all messages issued by ToExpression to a string-stream. Here is an example of that approach, with minimal error-checking: Needs["Developer"] interpret[str_String] := Module[{s = StreamToString[], r, m} , Block[{$Messages = {s}}, r = ToExpression[str, InputForm, HoldComplete]] ; m = StringFromStream[s] ; Close[s] ; &... 18 Description In software engineering, it is a good practice to comment your code. I would advise you to utilise comments to partition your code in a following way. Additionally, if you have ever worked with any other programming languages, you could employ indentation. Alternatively, you could modularize your application as to develop it in smaller, more ... 17 I appear to be in the minority but I never write big blocks of Mathematica code, it's just too difficult to read. The way I look at it you have to consider how a reader will understand your app. So I make the main block very small, like this: Manipulate[ Row[{ vectorPlotAndTrajectory[y0, b], Show[fy, position[y0, b]] }] , {b, -7.99, 8} , {{y0, 2}... 16 There is a difference between SetDelayed (:=) with or without semicolon, but it usually doesn't matter. The semicolon is actually a CompoundExpression which has Null as its last element - that's the one that gets returned. But SetDelayed returns Null, too. So the results returned with or without semicolon are the same. An exception to this statement ... 13 Intermingling Operator Forms & Linguistic Connections This answer attempts to draw out linguistic connections in understanding why operator forms seem so useful, a process that can perhaps point to further utility. Operator Forms are a type of modularization with the standard re-use and combinatory advantages but IMO the biggest benefit is cognitive - ... 13 You can use the Notation package. It requires a GUI palette though. Needs["Notation"] Once you have this package loaded, you can use the template to define: Notation[+[x___] ==> Plus[x___]] and then +[1,2,3] (* 6 *) Similarly, Notation[*[x___] ==> Times[x___]] and so *[2,3,4] (* 24 *) Note: A * typed as the first character of a cell ... 13 Personally, I use lots of newlines and let the Front End indent. There doesn't seem to be anything special in your code, other than a lot of nesting that is in fact necessary in this case. You were using Grid incorrectly. Grid[a,b] is wrong. Grid[{{a,b}}] or Grid[{{a},{b}}] are correct. I guess you wanted Column[{a,b}], so I changed that. Using Text ... 11 My offering: And @@ Or @@@ Outer[Equal, {x, y, z, m}, {2, 3, 4}] 11 You can rewrite your idea using If as follows: If[MemberQ[elList, #], 1., 1.13] & /@ els {1.13, 1., 1., 1.13, 1.} However you may find on larger problems that repetitive use of MemberQ is not as fast as you would like, so consider a hash table in the form of an Association, or if using an older version of Mathematica a Dispatch table. Create a ... 10 This is following Jens's suggestion to use a different Unicode glyph, but different from the answer linked in the corresponding comment. We can use Unicode directly, so let's just find a letter-like modifier glyph that looks good. A quick search for "prime" gives a nice solution in MODIFIER LETTER PRIME. You can type it using the notation \:02b9 which ... 10 One can use Trace for this. I stuck in an extra, different error to show it is omitted in the output. It should also be clear how to trace other error messages. foo = Trace[ Table[ a = 1/0; b = {2, 3} c = i;, {i, 2}], HoldPattern@Message[Set::write, ___], TraceAbove -> True] (* Power::infty, Set::write errors *) If you would ... 10 To summarize the comments into an answer: The second element is a list of lists because there may be several different tags sown. For example, Reap[Sow[1, x]; Sow[2, y]; result] (* {result, {{1}, {2}}} *) Another example by belisarius, Reap[Sow[1, {x, y}]; Sow[2, y]; Sow[3, x], _, tag] (* {3, {tag[x, {1, 3}], tag[y, {1, 2}]}} *) See also this ... 10 Riffle does not have the HoldAll or HoldRest attribute: Attributes[Riffle] {Protected} The documentation for SetDelayed says that lhs:=rhs returns Null if the assignment specified can be performed, and returns$Failed otherwise. So what happens in your first example is that the second argument evaluates to Null before it is passed to Riffle. ...

9

Well, the following meets your formal requirements evenFunction[f_][args__] := f[Abs /@ Unevaluated[args]] evenFunction[even][a, b, c] even[Abs[a], Abs[b], Abs[c]] But is it really better than evenFunction[f_][args__] := f @@ Abs[{args}] I, myself, would choose the 2nd version over the 1st. Update It is not necessary to set the attribute ...

9

For Plus, there's this, from How would I add together any list of arguments as a pure function?: +Sequence[1, 2, 3] (* 6 *)

9

Imo the most common/readable/flexible way: Function[h, h[#, Log[#]] &][myF] /@ {7, 3} and for fun, less general, as pointed in comments: Through@*#[Identity, Log] &[myF] /@ {7, 3} which can be even shorter, thanks to ybeltukov Through@*#[# &, Log] &[myF] /@ {7, 3}

9

It turns out the Olivier function is already built-in, but in a disguised form: With[{n = 3, a = 2}, Table[j! SeriesCoefficient[1/MittagLefflerE[n, x^n]^a, {x, 0, j}], {j, 0, 40}]] {1, 0, 0, -2, 0, 0, 58, 0, 0, -6218, 0, 0, 1630330, 0, 0, -847053482, 0, 0, 766492673914, 0, 0, -1106653345942538, 0, 0, 2392407356983116538, 0, 0, -...

8

394 Unicode characters that are not valid Mathematica Symbols can be found from ss = Quiet@Table[{tem = "\\:" <> IntegerString[i, 16, 4], Symbol[tem]}, {i, 0, 16^4 - 1}]; notsym = Cases[ss, {z_, Symbol[__]} -> z] The list is largely but not entirely complete. To obtain the printable symbols for many of these unicodes, use Map[FromCharacterCode[{...

8

Simpler example: D[u', u] (* 0 & *) Usually it helps to inspect the FullForm of an expression to understand how Mathematica works. u' // FullForm But comparing the two similar forms, Derivative[1][u] and x[1][u], it's hard to understand what is happening. D[Derivative[1][u], u] // Trace D[x[1][u], u] // Trace D[x[1][u], u] /. x -> ...

8

Yes, you can use only pure functions: f = ## &[#, Log@#] & /* # &; f[myF] /@ {7, 3} (* {myF[7, Log[7]], myF[3, Log[3]]} *) It can be shorter with a bit different syntax: g = ## &[#, Log@#] &; g /* myF /@ {7, 3} (* {myF[7, Log[7]], myF[3, Log[3]]} *)

8

If you look at the FullForm of the expression {}, you see that you're not assigning "nothing", but an empty List: abc = {} is the same as abc = List[] That is, a List of zero length. {a, b, c, …} is merely syntactic sugar for List[a, b, c, …], so {} is syntactic sugar for List[]. What is a List? It's literally anything that has the Head List, so a ...

8

And @@ Thread[{a, b} > {c, d}] (* a > c && b > d *)

8

PlusMinus formats nicely, but it does not have a built-in meaning. You may work around that: h = 0.08; t = 0.13; Plot[ Evaluate[ Sqrt[(1/4) + (1/64 h^2) - x^2] - (1/8 h) + {-1, 1} ((3/8) t (1 - 2 x) Sqrt[1 - (2 x)^2]) ], {x, -.5, .5} ] If need be, you could also define your own meaning for PlusMinus: Clear[PlusMinus] PlusMinus[a__] := {-1, 1} (a)...

7

As mentioned in my comment to the question, I think the best solution is to use a Unicode character (see also the answer by The Vee). Here is a modified version of my earlier answer: SetOptions[EvaluationNotebook[], InputAliases -> DeleteDuplicates@ Join[{"'" -> FromCharacterCode[700]}, InputAliases /. Quiet[Options[...

7

Look at the Possible Issues section of the documentation for Sort: "Numeric expressions are sorted by structure as well as numerical value" list = {1/2 (1 + Sqrt[5]), 1, 1, 1/2 (1 - Sqrt[5]), 0}; The approach recommended there to Sort by numerical value only is sorted = Sort[list, Less] (* {(1/2)*(1 - Sqrt[5]), 0, 1, 1, (1/2)*(1 + Sqrt[5])} *) ...

7

Module[{i = 1}, Nest[1 + x^n (#) &, 1, 3] /. n :> i++] Or (same result) Fold[1 + x^#2 #1 &, 1, {3, 2, 1}]

7

How about this?: Evaluate@Grad[#1 + #2^2, {#1, #2}] & (* {1, 2 #2} & *) Or for pure obfuscatory fun: I'd like to reinstate to my first answer (see edit history), Evaluate@Grad[#1 + #2^2 &[#1, #2], {#1, #2}] & even though #1 + #2^2 &[#1, #2], which equals #1 + #2^2 and seemed redundant, because it has the right general form, ...

7

What I think you want: S = {s1, s2, s3}; x = {1, 2, 4, s1, y}; Intersection[x, S] Outputs: {s1} As for # see http://reference.wolfram.com/language/tutorial/PureFunctions.html For your edited question: set = {s1, s2, s3}; x = {1, 2, 4, s1, y, f1[s1], f2[s2]} p = Alternatives @@ set; Cases[x, p | _[p]] {s1, f1[s1], f2[s2]} Reference ...

7

To be very explicit and related to the situation in your question, consider this simple function f and the different fs that all might be ways to compute the derivative at a first glance. ClearAll[f, fs, fs2, fs3, fs4]; f[x_] := x^2; fs[x_] := D[f[x], x]; fs2[x_] := f'[x]; fs3[x_] = D[f[x], x]; fs4[x_] := Block[{t}, D[f[t], t] /. t -> x]; Let's check ...

Only top voted, non community-wiki answers of a minimum length are eligible