New answers tagged

3

The troublesome part here is Abs is not symbolically differentiable: D[Abs[c], c] % /. c -> 1 Abs'[c] Abs'[1] (* doesn't make sense *) To circumvent this, one way is to use ND instead of D: Needs["NumericalCalculus`"] ND[Abs[a + b I], a, 1] /. b -> 1 0.707107 Another way is to use ComplexExpand before differentiation: D[Abs[a + b I] // ...


1

Wolfram finally (V10.2) has made a function SubstitutionSystem that does the job. These are examples from documentation (see Applications part). ArrayPlot[ SubstitutionSystem[{1 -> {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, 0 -> ConstantArray[0, {3, 3}]}, {{1}}, 5][[-1]]] And using @Mr.Wizard notation with CrossMatrix from the answer above: Image3D[ ...


3

You have made the simple mistake of not being thorough enough when you wrote out the function-based version of your code. Only one function definition need be changed. Q[n_] := ArrayFlatten[mat[n] /. {a -> A[n], b -> B[n], y -> Y[n], z -> W[n]}] With this change h[3] returns 1.


0

I have probably missed the point but wanted to play: f[lst_, ru_, lab_] := Module[ {r, u, s}, r = Boole@Through[ru[#]] & /@ lst; u = (lab # /. {0 -> Sequence[]} & /@ r) /. {{} -> 1}; s = StringJoin @@@ u; Thread[{lst, s}] /. {{_, y_String} :> y, {x_, 1} :> x} ] So, f[Range[100], {Mod[#, 2] == 0 &, Mod[#, 3] == 0 &, ...


3

This does not answers my own question fully (I am still interested to see if someone might come up with an truly elegant solution based on DownValues) but I found a rule-based solution that is imho. elegant non the less. fizzbuzz[rls_]:= With[{res=ReplaceList[#, rls]}, If[res=={}, #, StringJoin@res]]& fizzbuzz[{_?(Mod[#,2]==0&) -> "Fizz", ...


2

OP's question Updating this post, the set of functions related to HashTable is in version 10.2: $ContextPath = Prepend[$ContextPath, "System`Utilities`"]; ?*HashTable* HashTable HashTableContainsQ HashTableMapAt HashTableSet HashTableAdd HashTableGet HashTableRemove and is extended in version 10.3 to: ?*HashTable* HashTable ...


3

An alternative approach It seems at its heart that you want more than one rule to match a given expression and each match to return a value, which is then to be combined. I cannot think of any "clever" method to do this that is not contrived and questionable. Perhaps something more standard has at least some service for you. Your rules, each one in a ...


7

In keeping with the multiple DownValues part of the question, this is an option: ClearAll@f conditions[x_] := {Mod[x, 2] == 0, Mod[x, 3] == 0, x < 10} f[x_ /; Or @@ conditions[x]] := StringJoin@Pick[{"Fizz", "Buzz", "Zapp"}, conditions[x]] f[x_] := x It tests for any of the conditions first, and then within the body of the function uses the individual ...


0

Perhabs g[x_] := Switch[{Mod[x, 2] == 0, Mod[x, 3] == 0, x < 10}, {True, True, True}, "FizzBuzzZapp", {True, True, False}, "FizzBuzz", {True, False, True}, "FizzZapp", {True, False, False}, "Fizz", {False, True, True}, "BuzzZapp", {False, True, False}, "Buzz", {False, False, True}, "Zapp", _, x] Not sure about elegance but at ...


2

f[x_] := With[{res = f1[x] <> f2[x] <> f3[x]}, Switch[res, "", x, _, res]] f1[x_?(Mod[#, 2] == 0 &)] := "Fizz" f1[x_] := "" f2[x_?(Mod[#, 3] == 0 &)] := "Buzz" f2[x_] := "" f3[x_?(# < 10 &)] := "Zapp" f3[x_] := "" f /@ Range@15 {"Zapp", "FizzZapp", "BuzzZapp", "FizzZapp", "Zapp", "FizzBuzzZapp", "Zapp", ...


2

n = 0; a = 1; L = 0; b = 1/100; m = 1/2; g = (2 m a r)/(n + L + 1); f[r_] = AiryAi[(2 m b)^(1/3) r]; r0 = r /. FindMaximum[r^(L + 1) f[r], r, WorkingPrecision -> 20][[2]] // Rationalize[#, 0] &; r0 // N // InputForm (* 4.103398736759 *) r1inv = Series[1/r, {r, r0, 4}] // Normal // Simplify; r0 == r /. Solve[r1inv - 1/r == 0, r, ...


0

Update Fully compiling the code to C makes it as fast as the built-in: cBernstein = Compile @@ (Hold[{n, {j, _Real, 1}, u}, Table[expr u^i (1 - u)^(n - i), {i, j}], CompilationTarget -> C] /. expr -> FunctionExpand[Binomial[n, i]]) BezierSurface4 = With[{cBernstein = cBernstein}, With[{AllBasis = Function[{deg, u0}, cBernstein[deg, ...


0

How about Graphics3D @ BSplineSurface[cpts] // AbsoluteTiming


1

Try this one. flip[a_, n_Integer] := Module[{pos} , pos = Union[#, Reverse /@ #] &@RandomSample[SparseArray[a]["NonzeroPositions"], n] ; ReplacePart[a, Thread[pos -> -2 + Extract[a, pos]]] ] It also makes the matrix symmetric. flip[Array[b, {3, 3}], 2] // MatrixForm You can put back RandomChoice in place of RandomSample if you want.


1

Simple syntactic errors. This works: flip[a_List , n_] := Block[{lis1, lis2, s, size}, size = Dimensions[a]; lis1 = Most[ArrayRules[a]]; lis2 = RandomChoice[lis1, n] ; lis2 = Map[ReplacePart[#, 2 -> -2] &, lis2[[1 ;; n]]]; s = SparseArray[lis2, {size[[1]], size[[1]]}, 0] + a] Don't use upper-case letters for variables or functions as they may ...


6

Since Mathematica 10, there is the TypeSystem` Context, that is nearly what you might be looking for. It is just a wrapper around patterns. TypeSystem`ConformsQ[ {1, 2, 3}, TypeSystem`Vector[TypeSystem`IntegerT, 3] ] (* --> True *) It is the thing being used internally by Dataset-related functions. (Maybe one should say something like Dataset[{}] ...


2

Here are solutions to both boundary protocols. They are built on the same basic framework -- mainly the function that generates the moves for the walker is what differs between the two. There is a little adjustment in the way the lines and walker point is drawn because of discontinuities in the path generated by the wrap-arround protocol, Path clips at the ...


6

You could use Region functionality (for simpler regions), e.g. rw[pt_, s_, n_, reg_] := Module[{ch = {{0, 0}, {1, 0}, {-1, 0}, {0, 1}, {0, -1}}, np, st}, st = RandomChoice[ch, n]; FoldList[If[RegionMember[reg, #1 + s #2], #1 + s #2, #1] &, pt, st] ] an[p_, step_, num_, regn_] := With[{pnts = rw[p, step, num, regn]}, ListAnimate[ ...


1

Try constraining the domain to Reals Solve[{a == 0, b == 0}, {M, P},Reals] You may want to look also into Reduce Reduce[{a == 0, b == 0, M > 0, P > 0}, {M, P}, Reals]


2

If you are expecting Clear[x] to clear definitions of x, don't expect anything more from Clear[s]. So you can go with: X = 5; s = "X"; Clear[#] &@s X X Or, as pointed by Albert Retey, with Clear[Evaluate@s]. Maybe you don't want to use strings, then: X = 5; s = ToExpression["X", StandardForm, Unevaluated]; Clear[#] &@s X works too. Some ...


3

This references a past dialog on the subject of argument testing. There may be a good reasons for the side-effect method you are using, but in this case a check function may simplify things. func::invidx = "Index `1` should be a non-negative machine-sized integer betwwen `2` and `3`."; func::intnm = "Number `1` should be a non-negative machine-sized ...



Top 50 recent answers are included