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

ClearAll[f, g] f = ConditionalExpression[(1/27)*(#^4 - 6 #^3 + 12 #^2 + 19 #), 0 <= # <= 5] &; g = InverseFunction[f]; Grid[Partition[#, 2] &[Plot[#, {x, 0, #2}, PlotLabel -> #3, ImageSize -> 300] & @@@ {{f[x], 5, "f[x]"}, {g[x], 10, "g[x]"}, {{f[x], g[x]}, 5, "{f[x], g[x]}"}, {f[x] - g[x], 5, "f[x]- g[x]"}}]] Update: ...


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


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

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


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

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

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

Maybe you can use the following two constructs to your advantage, which will keep the Conjugate, but evaluate and simplify the derivative inside. Using ReleaseHold, you can then evaluate even the Conjugate. Note that I left out the divisor in the Conjugate-case for clarity, but you can easily add that into the second function's definition. d[g_] := ...


2

The answer is yes, and you can also test it. 1/RandomInteger[10^200] + 1/RandomInteger[10^200] /. Rational[a_, b_] :> GCD[a, b] 1


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]}


2

funcs = {Sin[x], Cos[x], Sin[x] Cos[x]}; tips = {"t1", "t2", "t3"}; Plot[Thread[Tooltip[funcs, tips]], {x, -2 Pi, 2 Pi}, Evaluated -> True] Plot[Evaluate@Thread[Tooltip[funcs, tips]], {x, -2 Pi, 2 Pi}] Plot[Evaluate@MapThread[Tooltip, {funcs, tips}], {x, -2 Pi, 2 Pi}] Plot[Tooltip @@@ Transpose[{funcs, tips}], {x, -2 Pi, 2 Pi}, Evaluated -> True] ...


2

Lacking an example of your polynomial I did not attempt to test this extensively, but try: ClearAll[fn] fn[p_Plus] := fn /@ p fn[f_ g_] := fn[f] g + f fn[g]


2

The default for all functions that have the option TimeZone is $TimeZone. Setting it to a different value should change it for all these functions. Unprotect[$TimeZone] $TimeZone = -0 Protect[$TimeZone] Sets the time zone to Greenwich Mean Time (GMT).


1

As pointed out in the comments by @LLIAMnYP, FindRoot cannot solve one equation for two unknowns. You may be able to use FindInstance func = Plus; n = 4; (* number of variables *) var = Array[x, n]; x[1] = 5; (* fixed variable *) ns = 4; (* number of solutions *) For Integers solnI = FindInstance[ func @@ var == 0, Rest[var], Integers, ns] ...



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