Hot answers tagged

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


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


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[{}] ...


5

So, extending comments. Quick fix is to use Return[$Failed, Module] here but it's not the right approach. This SplineDegree should not reach MakeBoxes at all. Here's what should be done. Imo, the user should not use CAGDBSplineFunction manually to create it, like in case of InterpolatingFunction it is created by something else, here Interpolation. So in ...


4

Thanks a lot for Kuba's hints and his answer, I written down my understanding. Using two defintions: CAGDBSplineFunction[pts_, opts : OptionsPattern[]]:=, the user should use this definition. CAGDBSplineFunction[pts_, {deg_, knots_}]:=, which returns the object. CAGDBSplineFunction /: MakeBoxes[ obj : CAGDBSplineFunction[pts_, {deg_, knots_}], ...


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 work around 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] // ...


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.


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


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


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


3

If you're only interested in extracting expression from $\TeX$ code, and you don't care about formatting, then easiest thing to do is to remove unnecessary formatting from string with $\TeX$ code. I can see four formatting sub-string types to delete: opening and closing of align environment, newlines, and column separators. There's also full stop at the and. ...


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


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


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


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


2

This is a fairly common problem to encounter and in this case was a bit subtle due to having an outer and inner function both in need of being defined only for explicitly numeric input. So I'll repost my comment as an answer and make it a Community wiki. The FindMinumum objective itself needs to be defined only for numeric input. Which can be done as ...


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


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


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]



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