Hot answers tagged

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] ; ...


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 ...


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 ...


12

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 - ...


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

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

The documentation you linked to is more about how Infix is used to define output formats. You can find more information on how to use functions in infix form in the tutorials Special Ways to Input Expressions and Operator Input Forms. If you really want to use Partition in the infix form with an offset, you can use samplelist ~Partition~ Sequence[3, 2] ...


9

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 ...


9

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 ...


9

The Notation package is not necessary to use an infix form of \[Star] as that is handled automatically. Also I recommend PadRight for constructing your expression (reference Generating a matrix using sublists A and B n times). SetAttributes[Star, HoldFirst] Star[a_List, n_Integer] := PadRight[a, n*Length@a, a] {1, 2}⋆5 (* ⋆ is \[Star] *) {1, 2, ...


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}


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 ...


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 *)


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 /. ...


7

Unevaluated@Sequence[1, 2]~ConstantArray~10 $\ $ {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2} Or using Notation << Notation` Notation[ParsedBoxWrapper[ RowBox[{ RowBox[{"[", "const_", "]"}], "\[Star]", "reps_"}]] \[DoubleLongRightArrow] ParsedBoxWrapper[ RowBox[{ RowBox[{"Unevaluated", "@", RowBox[{"Sequence", "[", "const_", ...


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 ...


6

Brief? How about this. Define: c = ConstantArray; Now you can get what you want using the infix notation: "a"~c~7 and 10~c~7 With lists {1, 2}~c~7 you'll need to Flatten.


6

There's a lot of syntactic sugar in there. I'd like to add an answer to show you how you can make progress yourself when encountering unknown syntax. If you wrap the code in FullForm[Hold[...]] Mathematica will show you the functions all these operators stand for: FullForm[Hold[Range[10] /. {x_ /; PrimeQ[x] -> x^2}]] (* Hold[ReplaceAll[Range[10], ...


6

Range[10] - Make a list of integers from 1 to 10. /. - Replace all occurrences of {x_ - anything, to which we will refer to as x /; - so long as the test PrimeQ[x] - for primality yields True -> - with x^2} - the square of itself.



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