Tag Info

Hot answers tagged

17

Oleksandr is correct about the way evaluation works. a/b seems to be interpreted (parsed) directly as Times[a, Power[b,-1]], or more readably: $a\times b^{-1}$. Divide[a,b] is interpreted as is. Evaluation then proceeds from these forms, and the arithmetic is carried out differently for the two cases: either $a\times (1/b)$ or $a/b$. Here are some ...


13

While it would've been nice if the package handled it automatically, it can be fixed with a simple overloading of Quantity: Unprotect@Quantity; Quantity /: (0 | 0.) Quantity[_, unit_] := Quantity[0, unit] Protect@Quantity; You can add this to your init.m, so that you don't have to define it each time. You can test your examples with this: 0. Quantity[1, ...


10

Here's one idea. Hold the expression unevaluated and go up the expression tree from (near) the bottom, level by level, and evaluate. expr = HoldForm[1/((a + 2 b)/c^2)] /. {a -> 1, b -> 2, c -> 3} out = ToExpression@ToString[FullForm@#] & /@ (ReplacePart[expr, # -> Extract[expr, #] & /@ #] & /@ ...


8

Try this : s[n_] := Total[ Range[n]^2] to check how it works, e.g. : s[5] // Trace There is also a purely symbolic approach, e.g. : $\quad n^2$ ~ Sum ~$n\quad$ (see Infix notation) : (n^2) ~ Sum ~ n 1/6 (-1 + n) n (-1 + 2 n) Note : Sum[ n^2, n] returns the same as Sum[ i^2, {i, n-1}] does, i.e. indefinite sums starts at 0 while ...


8

Calculating eigenvalues involves solving for the roots of the characteristic polynomial, which is of degree equal to the order of the size of the matrix. When you input real numbers, it can search for the roots of the polynomial using numerical techniques. When you input exact integers (or rationals, probably) it tries to find exact answers for the roots of ...


5

Not a full answer since I need to sleep :) but more of an observation, which might help. It seems to have to do with the fact that 0 and 0. are not the same in Mathematica. This simple example shows it UnitConvert[0. + Quantity[5, "Meters"], "Inches"] (*--> UnitConvert[0. + Quantity[5, "Meters"], "Inches"] *) while UnitConvert[0 + ...


4

Independently I arrived at something similar to Michael's answer, yet different. I borrowed his formatting function after seeing it as it works better than what I had. Perhaps this will also be of use: evalFromBottom[expr_, lv_: 1] := If[lv > Depth@expr, expr, With[{ev = Replace[expr, x_ :> RuleCondition[x], {-lv}]}, If[expr === ev, ...


4

An oddball one using a recursive, memoizing function for the square. Clear[sq]; sq[n_Integer] := sq[n] = sq[n - 1] + n + (n - 1) sq[1] = 1; Sum[sq[n], {n, 6}] 91 It's not something I would directly use for such a goal, but you asked for something without explicit multiplications and you got it. Alternatively, if we don't interpret Dot as some ...


4

Ten ways to beat a dead horse Sunday afternoon on an airplane without wifi and this was the problem I remembered reading at breakfast. Forgive me for the time I had on my hands. All because @Aky resurrected this dead horse. (Thanks by the way. I'm glad to have figured out the CellularAutomaton one, but we landed before I could come up with a good MapAll ...


4

Here's a way to brute-force search for numbers that have the property that the sum of the digits raised to an integer power is equal to the number itself. list = {}; Do[If[Total[IntegerDigits[b^e]] == b, AppendTo[list, {b, e}]], {b, 2, 800}, {e, 2, 100}]; This returns a list of the numbers and powers (here's just the first 50), ordered so that they ...


4

As rasher and the documentation both say, Equal has a certain level of fuzziness. The same is true of SameQ, though it has a more stringent tolerance. The following computations are all done with machine precision numbers. Similar things should hold with arbitrary precision numbers but the analysis might be trickier. (* 12 zeros, difference = ...


3

See the documentation for Equal. There is a tolerance for inexact numbers. The order of operations combined with precision of targets can affect whether things fall "in" or "out" of tolerance. See specifically the "Possible Issues" section in the documents for Equal. As far as why results themselves differ in FP arithmetic, there is no better source than the ...


3

Here's a variation on partial81's answer: reps[n_, m_] := Flatten[ConstantArray[#, m] & /@ Range[n]] This creates an array from 1 to $n$, repeating each value $m$ times. For example: reps[5,3] {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5}


3

If exponentiation is allowed, how about this? w = 10; E^(Log[1/6] + Log[w] + Log[1 + w] + Log[1 + w + w]) 385 Explanation ClearAll[w] Sum[i^2, {i, 1, w}] 1/6 w (1 + w) (1 + 2 w)


2

With the suggested edits from Rojo in the comments above, the following is what answers my question: plus[args__] := Row[Riffle[{args}, " + "]] Then, Block[{Plus = plus}, x + 1 + i + 4 + z] // TraditionalForm returns:


2

Another way to do this: q = Range[6]; m = 3; Round[(q + 1)/m] {1, 1, 1, 2, 2, 2} Here's the same kind of idea made into a function (it's a little simpler to use Ceiling than Round: make $m$ copies of each number from 1 to $n$ reps2[n_, m_] := Ceiling[Range[n m]/m] reps2[3,4] {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3}


1

An extended comment. I'm not sure if this has been realized, please correct me if it has. The result of the Divide[a,b] operation is not the same as the first 3 which are identical. {a, b} = List @@ RandomReal[{-50, 50}, {2, 1*^7}]; x1 = a/b; x2 = a b^-1; x3 = a/b; x4 = Divide[a, b]; Now... Tally[x1 - x2] Tally[x2 - x3] Both give 10^7 zeros. ...


1

This provides a nice application for FindSequenceFunction, as suggested by Michael f = FindSequenceFunction[{5, 7, 9, 10, 13, 13, 17, 16}] 1/4 (14 + 2 (-1)^#1 + 7 #1 - (-1)^#1 #1) & You can check that this sequence f starts out as specified: f /@ Range[10] {5, 7, 9, 10, 13, 13, 17, 16, 21, 19} To calculate the sum of the first 40 terms: Total[f /@ ...


1

Here are a couple more ways, using Table and Array. In each case, r: the number of repeats n: the range from 1 to n f[r_,n_]:=Table[i,{i,r},{n}]//Flatten f[5, 3] {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5} g[r_,n_]:=Array[#&,{r,n}]//Flatten g[5, 3] {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5}



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