Tag Info

New answers tagged

3

Most user friendly approach, with function: mySet[l_] := {a, b, c, d, e} = l I'm not sure what is the general goal, but here's one way with UpValues: table /: Set[table, l_] := ({a, b, c, d, e} = l); table = Range[6, 10]; {a, b, c, d, e} {6, 7, 8, 9, 10} table = Range[1, 5]; {a, b, c, d, e} {1, 2, 3, 4, 5} Different approach: list := ...


1

Use a different format from the start to avoid this complication. L1 = {k, 2 k + 1, 3 k + 3, 4 k, 5 k - 4, 6 k}; L2 = {1, 2, 1, 1, 1, 2}; MapThread[{# > 0, # < 0}[[#2]] &, {L1, L2}] {k > 0, 1 + 2 k < 0, 3 + 3 k > 0, 4 k > 0, -4 + 5 k > 0, 6 k < 0}


3

Also: L2a = L2 /. {">0" -> (Greater[#, 0] &), "<0" -> (Less[#, 0] &)}; MapThread[Apply, {L2a, L1}] (* {k > 0, 1 + 2 k < 0, 3 + 3 k > 0, 4 k > 0, -4 + 5 k > 0, 6 k < 0} *) Or Apply @@@ Thread[{L2a, L1}] (* {k > 0, 1 + 2 k < 0, 3 + 3 k > 0, 4 k > 0, -4 + 5 k > 0, 6 k < 0} *) List /@ Apply @@@ ...


2

How about ClearAll[k] L1 = {{k}, {2 k + 1}, {3 k + 3}, {4 k}, {5 k - 4}, {6 k}}; L2 = {">0", "<0", ">0", ">0", ">0", "<0"}; result = MapThread[{ToExpression@StringJoin[ToString[First@#1], #2]} &, {L1, L2}]


2

You don't need a For loop -- David Stork suggested using Table. It is also possible to do the same thing using a "pure function": {#, Factor[x^# - 1]} & /@ Range[95, 105]


1

Table[{n, Factor[x^n-1]}, {n, 95, 105}] // MatrixForm


4

You asked for "more elegant or shorter" and Ymareth gave you exactly that. However it is worth noting that with long Alternatives this comes at a performance cost. Please observe: Needs["GeneralUtilities`"] set = Range[20000]; fn1[x_] := set /. (Alternatives @@ x -> -1) fn2[x_] := set /. (Dispatch @ Thread[x -> -1]) BenchmarkPlot[{fn1, fn2}, ...


0

MapAt on alternatives pattern: With[{ list = {a, b, c, d, e, f, g, h}, sub = {a, c, e, f}}, MapAt[f, list, Position[list, Alternatives @@ sub]]] Replace f with 12& for a constant-12 function.


4

I like using Thread for this kind of rules {a, b, c, d, e, f, g, h} /. Thread[{a, c, e, f} -> Table[12, {4}]] As @kguler suggests there is no need having a Table and keeping track of the length of the list also for a single value. So the following is better! {a, b, c, d, e, f, g, h} /. Thread[{a, c, e, f} -> 12]


10

You can have a single rule using Alternatives (|)... {a, b, c, d, e, f, g, h} /. x : (a | c | e | f) -> 12 Furthermore you can construct the rule on the fly... {a, b, c, d, e, f, g, h} /. x : Alternatives@@{a, c, e, f} -> 12 As noted by @Kuba below there is no requirement for the pattern to have a name (x) so... {a, b, c, d, e, f, g, h} /. (a | ...


2

the "bug": (*"this is a bug*)"*) the work around: (*"this is a solution\*)"*)


1

Based on the procedure outlined in the first Answer to 40396, the internal representation of (*"this is a bug*)"*) is RowBox[{RowBox[{"(*", "\"\<this is a bug\>", "*)"}], "\"\<*)\>"}] We see that (*"this is a bug*) is interpreted as a comment, and "*) as additional code that is syntactically incorrect. As other Answers here have ...


10

We already have some answers explaining the issue. Let me give a solution to your problem. Let's say you have the code RegularExpression["((re)*)"] that you want to comment out. Since we have nested comments in Mathematica, just use a pair of (* to prevent your issue: Although I don't know the internal implementation of Mathematicas parser, the ...


3

I would not call this a bug in the sense that the behaviour of the Front End and the Kernel (run in command line mode) are consistent with each other. Yes, this does make code like "asd *)" un-commentable, so you might call it bad design. But I wouldn't call it a bug. Contrast this with certain bugs where the Front End parses expressions differently from ...


10

This behaviour is very common, possibly near universal, in programming languages with matchfix comment syntax. The reason is that the contents of a comment sequence is presumed not to be code. Usually that presumption is correct, but not in this case. The general rule is You should be able to put anything inside a comment and the only special tokens ...


2

This happens because characters inside the comment are not actually parsed. Ponder these two code fragments: (* "This string contains the end of a comment *)" *) (* "This comment begins with a quote character *) How can the parser tell whether the quote is the start of a string or just a quote?


2

If y is a packed array then Dot should be quite fast: y = RandomReal[{0, 1}, {10^5, 2}]; x = 12.3; result = y.{{1, 0}, {0, 1/x}}; This takes about a millisecond on my not-very-fast PC.


1

It does work by slightly rewriting your code: First let's create a list of law rules of the form of {"1"-> ... , "2" -> ...} and save it to an .mx file in the home cloud directory: data = "21"; url = "http://www.gesetze-im-internet.de/bgb/xml.zip"; xmlres = Import[url, Import[url, {"FileNames", 1}]]; myX["enbez", {}, {par_String}] := par; ...


2

So, this is the case I've anticipated and Nitree pointed out in comments: If any of the test_i evaluated by Which give neither True nor False, then a Which object containing these remaining elements is returned unevaluated. This is clearly shown by: Which[ a, 1 + 1, False, 1 + 1, False, 1 + 1] Which[False, 1 + 1, a, 1 + 1, ...


2

You can either specify the control type as Setter: smax = Pi; superset = {{.2, 3}, {.3, 4}, {.5, 6}}; Manipulate[Module[{SI, PH, R, TH, Z, MER, si, ph, r, th, z, mer, b, smax}, {b, smax} = subset; {si[t_], ph[t_], r[t_], th[t_], z[t_], mer[t_]} = {SI[t], PH[t], R[t], TH[t], Z[t], MER[t]} /. Quiet@First@ NDSolve[{SI'[s] == 1/b + Sin[PH[s]] ...


10

The behaviour we see is due to the precedence of &, which is much lower than the precedence of /@. As a consequence, the expression Line /@ (Print[#]; #) is bound tightly together by the high precedence /@ infix operator, yielding the single argument to the low precedence & postfix operator. This means that the second expression is interpreted as ...


1

Actually, Map[Line, Map[(Print[#]; #) &, {{{2, 1}, {1, 1}}, {{-2, 1}, {3, 1}}}]] is equivalent to Line /@ ((Print[#]; #) & /@ {{{2, 1}, {1, 1}}, {{-2, 1}, {3, 1}}}) with both producing {{2,1},{1,1}} {{-2,1},{3,1}} {Line[{{2, 1}, {1, 1}}], Line[{{-2, 1}, {3, 1}}]} Note the extra pair of parentheses that I added. They are necessary so that ...


0

Try this: MyFunc[y__] := (D[f[x], {x, Length[{y}]}]/(Length[{y}])!) /. x -> First[{y}] Example: f[x_] := Sin[x] MyFunc[3, 5] (*-(Sin[3]/2)*)



Top 50 recent answers are included