Tag Info

Hot answers tagged

12

Your basic requirement is met with: safeExport[file_String, args___] := If[ ! FileExistsQ[file] || ChoiceDialog["File already exists. Overwrite?"], Export[file, args], $Failed ] What you describe as "attributes" (e.g. PlotRange -> All) are known as Options or named optional arguments. (See Attributes for a description of what that ...


10

As was pointed out above, this is a good summary of Mathematica's constrained optimization methods. Read through this if you want to know a lot more. A quick answer is below: The answer to your question is strongly dependent on the function you want to maximize. Convex functions can be maximized quite easily, with the error controlled by the PrecisionGoal ...


7

?? *`*IndexBy* Following the usual spelunking steps ClearAttrributes[IndexBy, {Protected, ReadProtected}] ?? IndexBy reveals the code that defines IndexBy. Simplifying (and ignoring argument type-checks) it is something like: indexBy[f_][expr_]:=Association[(f[#]->#)&/@If[AssociationQ[expr],Values[expr],expr]] indexBy[foo][Range[5]] (* ...


7

There are few ways to do this. The simplest might be p = {1 + t, 1 + t + t^2, 1 + t + t^3}; p[[n]] /. t -> .2 where n is the index. Or you can convert the list to actual functions like this p = Function[t, #] & /@ p And now you can write p[[n]][.2] as you wanted. Now for example, you can evaluate all these function for at some value ...


5

This appears to be a bug in MovingMap. The function is applied beyond the left edge of the list and then the result is discarded: echo[x_] := (Print[x]; x) MovingMap[echo, Range@5, {2}] {0,1} {1,2} {2,3} {3,4} {4,5} {{1, 2}, {2, 3}, {3, 4}, {4, 5}} I can see no reason to use the padding element 0 here. Interestingly when one ...


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

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


4

This may help Hillheight[x_] := (1/16 x^2 - 2 x + 80)/(1/16 x^2 - 2 x + 20)^2 Hill = Plot[(1/16 x^2 - 2 x + 80)/(1/16 x^2 - 2 x + 20)^2, {x, 0, 32},PlotRange -> {0, 8}]; BladeofGrass = ParametricPlot[{32, 1/5 + x}, {x, 0, 39/5}, PlotStyle -> {Green}]; line[xANT_, x_] := InterpolatingPolynomial[{{xANT, Hillheight[xANT]}, {32, 8}}, x]; ...


4

Thanks Mr.Wizard. I've filed a bug for this, #288440 if you have future correspondence with people inside the company about it...


4

{l1, l2} = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}} MapThread[DirichletConvolve[n, #1, n, #2] &, {l1, l2}] (* {1, 3, 4, 7, 6, 12, 8, 15, 13, 18} *)


4

A simple way is: y = x; y[[perm]] = x; y (* {2, 4, 6, 1, 8, 3, 5, 7} *)


4

ClearAll[eF] eF[z_: y] := Module[{j = 1, p = Partition[#, 2]}, First[Times @@ (z[Length@p] - p[[-1]]) Exp[Total[(Subtract[##] 2 z[j++] + {#-#2}^2) & @@@ p]]]] & Examples: ClearAll[a, b, c, d, e, f] eF[][{a, b, c, d}] (* E^((a-b)^2+(c-d)^2+2 (a-b) y[1]+2 (c-d) y[2]) (-c+y[2]) (-d+y[2]) *) eF[][{a, b, c, d, e, f}] (* E^((a-b)^2+(c-d)^2+(e-f)^2+2 ...


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


3

Observing the code reported in kguler's answer I note that it could be written more efficiently, if such an operation is desired. Specifically (f[#]->#)& cannot be compiled because the result is a Rule. It would be better to map f directly to the values, then use AssociationThread to construct the association. Proposal indexBy[f_][expr_] := expr ...


3

Use Solve the neat way (and see here): Solve[{f1 == f2}, x] {* {x -> 1} *} Solve[{f1 == f3}, x] {* {x -> -1} *} Plot[{f1, f2, f3}, {x, -5, 5}, PlotLegends -> "Expressions", Epilog -> {Red, PointSize[Large], Point[{{1, 0}, {-1, 2}}]}]


3

Here it is an overkill but maybe for general context it is worth to say this. It does not work because fs are not regions: f1 = ImplicitRegion[{y == -x + 1}, {x, y}] f2 = ImplicitRegion[{y == x - 1}, {x, y}] Solve[{k \[Element] f1, k \[Element] f2}, {k}] {{k -> {1, 0}}} I said an overkill because people usually do something like: Solve[{y == -x ...


3

As Sjoerd points out, a good place to start is by recognising that the line from the ant to the tip of the grass must be a tangent to the curve. So you can solve for the x positions of the ant where this is true: antx = x /. NSolve[(8 - f[x])/(32 - x) == f'[x], x, Reals] (* {14.9475, 8.62026} *) Here's a plot showing those points and the line-of-sight to ...


3

Here is an implementation that reads almost like a literal translation of the problem statement: stringToNumbers[x_] := ToCharacterCode @ ToUpperCase @ x - 65 autokeyEncrypt[x_String, key_Integer] := Module[{xlist, keystream} , xlist = stringToNumbers[x] ; keystream = Most @ Prepend[xlist, key] ; FromCharacterCode[Mod[xlist + keystream, 26] + 65] ...


3

Try this: p[m_, t_] := Table[Sum[t^i, {i, 0, n}], {n, 1, m}]; Then the list of three elements you gave in your question is obtained as follows: p[3, t] (* {1 + t, 1 + t + t^2, 1 + t + t^2 + t^3} *) and the first term of the list is p[3, 0.2][[1]] (* 1.2 *) Have fun!


3

f[d_, σ_, x_] := Module[{h, ϵ0, m, e0, α, κ, β, a, ϕ, e, n}, h = 6.62606957*10^(-34); ϵ0 = 8.8541878176*10^(-12); m = 9.10938291*10^(-31); e0 = 1.6*10^(-19); α = (3/ 5 2^(-1/3) (3/π)^(2/3))*((ϵ0* h^2)/(m*(e0)^(5/3)))*(d^(-5/3)*σ^(-1/3)); κ = (5*α/3)*(1/2)^(2/3); β = 4/(κ^(3/4)*Sqrt[5]); a = 4/(β*Sinh[β/2]); ϕ = (Cosh[β (0.5 - x)])/(β* ...


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

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


2

IMHO this is a Bug. You can prevent the error message in V10 using Quiet: series = Range[5]; MovingMap[Quiet[#[[2]]/#[[1]] - 1 &], series, {2}] You can also use Internal`PartitionMap, without Quiet: Developer`PartitionMap[#[[2]]/#[[1]]-1&, Range@5, 2, 1] {1,1/2,1/3,1/4} Post about PartitionMap


2

There may be a better way to accomplish this, but I think this illustrates what you are asking. i = 1; Clear[uq]; sequence = RandomSample[Range[10^4], 10^4] ; uq[param_] := uq[param] = sequence[[i++]] uq["cat"] uq["dog"] uq["horse"] uq["cat"] 561 4835 2597 561 .. supposing we need to recover the values, invert[n_] := (Select[ ...


2

Not sure I understood it properly. Instead of Animate, looking at slope variations plots may also help determine points near tangency. f[x_]=(1/16 x^2-2 x+80)/(1/16 x^2-2 x+20)^2; Hillheight[x_]:=(1/16 x^2-2 x+80)/(1/16 x^2-2 x+20)^2; Hill=Plot[Hillheight[x],{x,0,32}] BladeofGrass=ParametricPlot[{m/5+x, m x },{x,0,35},{m,.05,0.5},PlotStyle->{Green}]; ...


2

You can build your filter with UnitStep, Chop, or HeavisideTheta as mentioned in the comments to your question, f1[x_] := x UnitStep[x] f2[x_] := Clip[x, {0, x}] f3[x_] := x HeavisideTheta[x] All of these will give the same values when given the same arguments, but what of the cost? We don't want to be programmers who "know the value of everything but ...


2

I would do something like this: autoPTAtkHill[portionOfPT_, portionOfCT_, m_] := Module[{ptBlocks, ctBlocks, ctMatrix, ptMatrix, inversePTMatrix}, ptBlocks = Partition[stringToNumbers[portionOfPT], m]; ctBlocks = Partition[stringToNumbers[portionOfCT], m]; ctMatrix = {ctBlocks[[1]], ctBlocks[[2]]}; ptMatrix = {ptBlocks[[1]], ptBlocks[[2]]}; ...


2

list1 = {{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {1, 1, 1, 1}}; list2 = {1, 5, 5, 5}; Grid[Prepend[Transpose[{Row[{"(", Row[#, ","], ")"}] & /@ list1, list2}], {"Initial Vector", "Period"}], Dividers -> {2 -> True, 2 -> True}] or Grid[Join[{{"Initial Vector", "Period"}}, Transpose[{Row[{"(", Row[#, ","], ")"}] & /@ ...


2

or NonlinearModelFit[]. With this function you can also extract the errors and other stuff.


1

Try FindFit[]. This function can fit a data set to arbitrary functions.



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