Tag Info

Hot answers tagged

15

It seems that there is a significant overhead every time a color scheme is switched. Once a scheme is loaded each use is fast, but changing color schemes apparently unloads and reloads the mechanism. The result is that the speed of application is directly related to the frequency of switching. With sorted values there is only one switch and application is ...


10

Yes, they are considered pseudo-listable. Often they are implemented with something similar to f[x_, a_List] := f[x, #]& /@ a


9

You may already have discovered that something like g[#]& doesn't work - this is because Function has the HoldAll Attribute, so its argument (g[#] in this case) doesn't get evaluated. The solution is to force g[#] to evaluate. Rasher showed what one way to do that, by using Evaluate, whose specific purpose is to force evaluation of arguments that would ...


9

Most functions in Mathematica are actually global rules, with Function being an (very important) exception. Argument patterns in functions defined by rules are typically used as a flexible typing mechanism. You can go from completely untyped definitions (where you use patterns like _, __, etc.), to something in the middle (like e.g. {__List}), to completely ...


8

ReplaceAll ReplaceAll does not behave as a Listable head. If it did it would be broken. Consider: SetAttributes[brokenReplaceAll, Listable] brokenReplaceAll[{1, 2, 3}, {{2 -> "b"}, {2 -> "X"}}] Thread::tdlen: Objects of unequal length in brokenReplaceAll[{1,2,3},{{2->b},{2->X}}] cannot be combined. >> If it were Listable then arbitrarily ...


7

ClearAll[ruleToFunction, f1, f2]; ruleToFunction[func_] := Function[, Evaluate@func[Slot[1]]]; g[x_] := Piecewise[{{0, x < 8.}, {2.5, 8. <= x < 18}, {0, x > 18}}] f1 = ruleToFunction[g] ClearAll[g]; f1@10 f[x_] := x Sin[x^2] f2 = ruleToFunction[f] ClearAll[f]; f2@10


6

A computer is a finite machine and there is a limit to how well you can visually explore such functions. Perhaps it will give enough of a impression to sum only a few terms. Even 5 terms exceeds the monitor's ability to display all the turns. Plot[ Evaluate@Table[Sum[(1/2)^n*Cos[3^n*Pi*x], {n, 0, k}], {k, {1, 2, 5}}], {x, 0, 3}, PlotStyle -> ...


5

First a simpler way to get your first example: (m = ArrayFlatten[Transpose /@ {{v1, v2, v3}, {v4, v5, v6}}, 1]) // MatrixForm An alternative way to do @bill's undoing trick: (m2 = ArrayFlatten[Transpose /@ {{v1, ## & @@ Transpose[vNew]}, {v4, v5, v6}}, 1]) // MatrixForm Using ArrayReshape as an alternative to ArrayFlatten: ...


5

TL;DR; myArrayFlatten = Flatten /@ Flatten[#, {{1, 3}}] & {x,y,z} or {{x},{y},{z}} is considered column vector. Usage example: v1 = {1, 2, 3}; vNew = {{4, 5}, {6, 7}, {8, 9}}; v4 = {10, 11, 12}; v5 = {13, 14, 15}; v6 = List /@ {16, 17, 18}; { {v1, vNew, v1}, {v4, v5, v6, v1}, {vNew, vNew} } // myArrayFlatten // MatrixForm Flatten ...


5

Here's the problem in a nutshell: Module[{c1, c2 = foo[c1]}, c2 /. c1 -> value] (* ==> foo[c1] *) The point is, Module replaces c1 by a local variable in its body, but not in later assignments in the variable list. You can see what happens by just looking at the variables: Module[{c1, c2 = foo[c1]}, {c1,c2}] (* ==> {c1$184, foo[c1]} *) You ...


5

The question of what you want to happen when the condition is not met will determine how you should proceed. If you wish the function to remain unevaluated try Condition: f[x_] := (1 + 2^n) /; 2^n < x n = 7; f[100] f[200] f[100] 129 See: Placement of Condition /; expressions Using a PatternTest versus a Condition for pattern matching For ...


5

I'm not sure how you were using FunctionDomain, but running the following example gets your error: f[x_] := If[x < 0, x, x^3] FunctionDomain[f[x], x, Reals] (* FunctionDomain::nmet: Unable to find the domain with the available methods. >> *) Changing If to Piecewise works though. f2[x_] := Piecewise[{{x, x < 0}}, x^3] FunctionDomain[f2[x], ...


4

You say that func[{1,2,3}] doesn't work, but in Mathematica we can actually define such functions. We just have to write func[{a_, b_, c_}] := a^2 + b^2 + c^2; and boom, now it works. Now you can do func /@ list or func@Transpose@list The latter works because of something called "listability". You can also use that with your original definition of ...


4

You can include an iterator along with the function in the mapping: k = 0; (++k; f[#]) & /@ Range[100];


4

This isn't a problem you should try to solve automatically. Use good code hygiene and make sure you don't call private functions. You should (aim to) understand your code well enough that you don't get surprises like this --- you wrote it, and you know it better than anyone else. If there are surprises even to you, how will anyone else understand it?! ...


4

myfunc[x_] := Piecewise[{{c x^2 + 2 x, x <= 2}, {x^3 - c x, True}}]; lim1 = Limit[myfunc[x], x -> 2, Direction -> -1] lim2 = Limit[myfunc[x], x -> 2, Direction -> 1] sol = c /. First@Solve[{lim1 == lim2}, c] (*2/3*) Plot[myfunc[x] /. c -> sol, {x, 1, 3}, Epilog -> {Red, PointSize[.015], Point[{2, myfunc[2] /. c -> sol}]}] ...


4

This is a good question, but unfortunately there doesn't seem to be a perfect solution. You can use ToExpression, e.g. ToExpression["1.23"]. But: (1) this gives no error checking (2) it's a serious security risk if you obtain the string from users (and it can go things go haywire in general if the string comes from an unknown source) ...


4

Since, Attributes[Function] {HoldAll, Protected} Use Evaluate f = Function[{x}, Evaluate[(1 - # &)@*(1 - # &)@x]] Function[{x}, x] f@x x


3

we need to force Integrate function to evaluate w.r.t x ClearAll[f, g, x] g[x_] := 3 x^2 + 2 x f[x_] := Evaluate[20 x + Integrate[g[x], x]] f[2] (* 52 *)


3

Your first function can have complex values for x<0, so you can define: f1[x_] := Re[x^x + 8 x + 7]; f2[x_] := Log[2 x + 4.4] and then plot the functions together with the sum: Plot[{f1[x], f2[x], f1[x] + f2[x]}, {x, -1, 1},PlotLegends-> "Expressions"}] But with the Range -10<x<10 you won´t see much left of the y-axis, due to the growth of ...


3

When you define a function f, you are (a) applying Set or SetDelayed to a pattern, (b) creating a rule, (c) associating the rule with a symbol (f), and (d) storing that in DownValues (usually). FullForm[Hold[f[x_] := x^2]] (* Hold[SetDelayed[f[Pattern[x, Blank[]]], Power[x, 2]]] *) f[x_] := x^2 DownValues@f (* {HoldPattern[f[x_]] :> x^2} *) First ...


3

How about just breaking up the matrix and then reconstructing the same way. Here are your source vectors v1 = {1, 2, 3}; vNew = {{4, 5}, {6, 7}, {8, 9}}; v4 = {10, 11, 12}; v5 = {13, 14, 15}; v6 = {16, 17, 18}; So now define and reconstruct: {v2, v3} = Transpose[vNew]; ArrayFlatten[{Transpose@{v1, v2, v3}, Transpose@{v4, v5, v6}}, 1] // MatrixForm If ...


3

first = {1 + a, 2 + a, 3 + a, 4 + a}; t = {0, 1, 2, 3}; MapThread[#1 /. a -> #2 &, {first, t}] (* {1, 3, 5, 7}*) or #[[1]] /. a -> #[[2]] & /@ Transpose@{first, t} (* {1, 3, 5, 7}*)


3

Before I begin, a quick note. In the problem as stated, the optimal value of c is $-\infty$, since that makes the value of $f-(-\infty)g=f-\infty=-\infty$ (since $g<0$ for all $x\in(0,1)$). I'll instead work the problem of minimizing $|f-cg|$. First we can evaluate the Maximize statement, just to see what we get: Maximize[{Abs[f[x] - c g[x]], 0 < x ...


2

The reason why the second method is failing is because you are looking for a pair whose second element is the minimum over the whole list (including all the first elements). This of course will fail whenever the global minimum is in the first position. The proper syntax is Cases[list, {_, Min[list[[All, 2]]]}] The speed improvement, on the other hand, is ...


2

The fastest I can come up with is (by separating finding the minimum second value in each pair): AbsoluteTiming@With[{min = Min@list[[All, 2]]}, Cases[list, {_, min}]] (* {1.288129, {{0.555911, 1.05947*10^-6}}} *) while MinimalBy takes much longer: AbsoluteTiming@MinimalBy[list, #[[2]]&] (* {2.074207, {{0.555911, 1.05947*10^-6}}} *) Your second ...


2

Adapting listMaxArg from linked topic seems to be the fastest. list = RandomReal[1, {10^6, 2}]; list[[Ordering[list[[All, 2]], 1]]] // AbsoluteTiming {0.010000, {{0.817248, 6.71112*10^-7}}}


2

Just for fun: f = Total[IntegerDigits[#]^2] &; fun[n_] := NestWhileList[f, n, Unequal, All] r = fun /@ Range[100]; gf[u_] := DirectedEdge @@@ Partition[u, 2, 1] Graph[Union[Join @@ (gf /@ r)], VertexSize -> 0, GraphLayout -> "SpringEmbedding", VertexLabels -> Placed["Name", Center], VertexLabelStyle -> Directive[Blue, 20, Background ...


2

Question is a bit ambiguous, but I think you're after: ff = Join @@ Most@FixedPointList[ NestWhileList[f, f[Last@#], FreeQ[{1, 89}, #] &] &, NestWhileList[f, #, FreeQ[{1, 89}, #] &]] &; ff[20] ff[15] (* {20, 4, 16, 37, 58, 89, 145, 42, 20, 4, 16, 37, 58, 89} {15, 26, 40, 16, 37, 58, 89, 145, 42, 20, 4, 16, 37, 58, 89} *) ...


2

Use Apply. The following will work: func @@@ list Example: In[1]:= f @@ {1, 2, 3} Out[1]= f[1, 2, 3] In[2]:= f @@@ {{1, 2, 3}, {4, 5, 6}} Out[2]= {f[1, 2, 3], f[4, 5, 6]}



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