Tag Info

Hot answers tagged

36

Control the Precision and Accuracy of Numerical Results This is an excellent question. Of course everyone could claim highest accuracy for her product. To deal with this situation there exist benchmarks to test for accuracy. One such benchmark is from NIST. This specific benchmark deals with the accuracy of statistical software for instance. The NIST ...


19

Stan Wagon presents a little utility function in his book Mathematica in Action called IntegerChop[]. Here's a slightly wrinkled version: IntegerChop = With[{r = Round[#]}, r + Chop[# - r]] &; You might wish to do comparisons yourself (the computer I am using does not have Mathematica). Here are some benchmarks: Do[roundif /@ testdata, {10000}]; ...


15

Here is a definition for mixedForm that works for all cases, i.e. proper and improper fractions and integers. Clear[mixedForm] mixedForm[Rational[x_, y_]] := If[Abs@x > y, HoldForm[#1 + #2/y], x/y] & @@ (Sign@x QuotientRemainder[Abs@x, y]) mixedForm[x_Integer] := x Some examples: mixedForm /@ {2, 4/5, 10/3, -3/4, -5/2} Out[1]= {2, 4/5, 3 + ...


13

One way is to plot the function 0 against a log axis. LogLogPlot[0, {t, 1, 12}, Axes -> {True, False}, Ticks -> {Range[12]}] or, changing the numbers LogLogPlot[0, {t, 64, 96}, Axes -> {True, False}, Ticks -> {Range[64, 96]}] The Axis function turns off the vertical axis (because you just want the number line) and the Ticks specifies ...


12

IntegerDigits works Try powers = IntegerDigits[204, 2] {1, 1, 0, 0, 1, 1, 0, 0} Now, if you want that formatted as a sum of powers of two, you have to hold it. For example Total@MapIndexed[#1 Defer[2]^(First@#2 - 1) &, Reverse@powers] 2^2 + 2^3 + 2^6 + 2^7 EDIT Nicer code, given that your numbers go up to 255 pow2[num_]:=Inner[#1 ...


11

Thanks to Nasser M. Abbasi i found a way. To change the Display. The function that you can provide for any ~Legend via LegendFunction wraps the complete ~Legend[] into anything. And he mentioned, that NumberForm encapsulates the numbers. So why not replace them (delayed)? Version 1: Scientific Notation at every Label Hence choosing f[x_] := x /. ...


10

EDIT As Chris pointed out Floor used this way fails for cases where the value is slightly less than a whole number, whereas Round works. I shall edit the remainder of my answer to correct this oversight. If you are using x == 0 you shouldn't need Chop since it is already making a numeric comparison: If[# - Round[#] == 0, Round[#], #] & Or simply: ...


10

You are losing hugely due to a base 10 implementation. Integers are manipulated in base 2 in Mathematica. So you would want a base 2 variant to get any reasonable behavior. Here is one way to code it. There might be tweaks that improve it. fastSquare[a_] := Catch[Module[ {len = BitLength[a], len2, hi, lo, h2, l2, hl}, If[len < 100, Throw[a^2]]; ...


8

My original answer is incorrect -- it is preserved as a record of my own hubris. :^) Simply, as Rojo points out, the calculation is still being done with 1*^1000, it's just being done at a different time. One may see this by manually observing the time taken for evaluation on an idle machine, or by setting this option which will print the total time taken ...


8

Some of the above don't work in some cases due to machine approximation, e.g. x = 6250*0.292 1825. If[# - ⌊#⌋ == 0, Round@#, #] &[x] 1825. Chop[# - ⌊#⌋] + ⌊#⌋ &[x] 1825. IntegerPart@# + Chop@FractionalPart@# &[x] 1825. But Stan Wagon's method works: With[{r = Round[#]}, r + Chop[# - r]] &[x] 1825


8

I'm surprised there isn't a question about this (i.e. entering numbers in scientific notation) already. To enter $3\times10^{-3}$, you can write 3*^-3. For further reference, see Input Syntax: Numbers.


7

Ok, if you want it faster still, and your close to integer numbers are machine-size integers - here are two equivalent implementations - in Mathematica compiled to C, and Java. It is an interesting problem to compare performance, we will observe that Java code is speed-equivalent to C code here, modulo small extra time needed for data transfer. The idea is ...


7

Another solution based on FractionalPart and IntegerPart would be : Fraction[x_Rational]:= Function[{z, y}, If[z!=0, HoldForm[z + y], HoldForm[y]], {HoldAll}] @@ {IntegerPart[x], FractionalPart[x]} Fraction[x_Integer] := x This approach produces slightly different results than R.M.'s solution : Fraction /@ {2, 4/5, 10/3, ...


6

Using the function from the notebook here: ImproperForm[x_] := Function[{z, y}, HoldForm[z + y], {HoldAll}] @@ {Floor[x],x - Floor[x]} Usage: In[12]:= ImproperForm[10/7] Out[12]= 1+3/7


6

This isn't directly an answer, and I'll delete it if it is off target. But you might want to use some non-System` context functionality for taking polynomial-mod-2 products. Specifically this works with integer lists of coefficients. I'll show an example below. In[1110]:= SeedRandom[1111]; vals = RandomInteger[2^8 - 1, 2] intlists = ...


5

And another way: logLine[min_, max_] := Module[{lines, labels}, lines = Line[{{Log[#], -1}, {Log[#], 1}}] & /@ Range[min, max]; labels = Text[#, {Log[#], 1.7}] & /@ Range[min, max]; Graphics[{ labels, Line[{{Log[min], 0}, {Log[max], 0}}], lines }, AspectRatio -> 1/10 ] ] We have then that logLine[1,12] yields ...


5

I have faced this problem earlier but failed attempts with simple operations based on NumberForm, Round.. have forced me to stop looking for general solution. I have thought my skills in MMA were too low, but also today I am not able to do this in simple way. (haven't I learned anything? :)) This form of expression uncertainty in measurement is described ...


5

Here is an attempt to implement both formatting for entire expressions and basic arithmetic. EDIT: fixed according to Artes' solution. ClearAll[mixedForm] Format[mixedForm[x : Except[_Rational]]] := x /. q_Rational :> mixedForm[q] Format[mixedForm[q_Rational]] ^:= Interpretation[ If[Abs[q] < 1, q, HoldForm[# + #2] & @@ {IntegerPart@q, ...


5

This should work with any real, rationalizing it (so, it becomes an approximate result)... The integer part of a negative number is considered the floor of the number. I'm not sure what's desired in those cases... ClearAll[MixedForm]; Format[MixedForm[r_?NumericQ]] := Module[{rat = If[Element[r, Rationals], r,Rationalize[N@r, 0]], x, y}, {x, y} = ...


5

Well, you may know that they're hexadecimal numbers, but Mathematica doesn't since you didn't tell it. When you enter hexList = {12, 10, 6b, 3f, 92} you'll notice that the "b" and the "f" are in blue, while the numbers are black. Also notice a small space between "6" and "b", and between "3" and "f". The blue means they're undefined symbols. Mathematica ...


4

Are you perhaps looking for AlgebraicNumberPolynomial? AlgebraicNumberPolynomial[AlgebraicNumber[1/2 (1 + Sqrt[5]), {-3, 2}], HoldForm[q]] -3 + 2 q The question states: "... show algebraic numbers" and "I would like to view ..." If you would like to display the AlgebraicNumber expression this way but retain its full syntax you can use a formatting ...


3

The code in your question as presently shown does not produce the result that you describe. All I can figure is that you are entering input like this: hexList = {16^^12, 16^^10, 16^^6b, 16^^3f, 16^^92}; If that is the case you need to understand that these numbers are immediately and transparently converted on parsing; this is merely an input shorthand. ...


3

Simply ... dataConv = IntegerPart /@ data; Head /@ dataConv {Integer, Integer, Integer} But beware of machine precision: x = 6250*0.292 IntegerPart[%] 1825. 1824 May be better to use Round as suggested.


3

Representation of numbers of handled with NumberForm. Choice of exponent to display is determined by its ExponentFunction option. If you never want an exponent displayed, this option should be a function that always returns Null, i.e. (Null &): NumberForm[1.*10^-8, ExponentFunction -> (Null &)] (* 0.00000001 *)


2

Per J.M. suggestion, I've got a ten fold increase in speed: TotalLen = IntegerLength[a]; RightLen = Quotient[TotalLen, 2]; Print[First[Timing[{A, B} = QuotientRemainder[a, (10^RightLen)]]]]; Timing: 0.344 Although the time decreased but it is yet so large just for pre-processing.


2

This is just one of a range of problems in importing data from Excel (don't let me count the ways).... I wasn't aware of Rationalize, but here's a Rule that can be applied upon import to round numeric data: ExcelImportRule = (x_ /; (NumericQ[x] && (Abs[x - Round[x]] < $MachineEpsilon)) :> Round[x])


2

I solved my problem particular and wrote proper function: f[value_, mu_, k_] := Block[{n}, n = Ceiling[n /. NSolve[mu == 10^n, n]][[1]]; N[Round[{value, mu}, 10^(n - k)]] ] Function returns two element list. The first element is proper round value, the 2nd is mu. The "k" call parameter determine how many first digits of mu is important.


2

Question: When you want to use this in the file name, why don't you use another character as NumberPoint? ToString[NumberForm[N[8/3], {5, 2}, NumberPoint -> "_"]] (* "2_67" *) This can easily be included into a file name without problems. To answer your question I have to start again with another question: Is there a special reason why you don't ...


2

The code used in the demonstration is like this. Manipulate[ Graphics3D[ Point[pointCoordinates], ViewPoint -> UserView, ImageSize -> {450, 400}], { {NumPoints, 5000, "number of points"}, {100 -> "100", 500 -> "500", 1000 -> "1000", 2500 -> "2500", 5000 -> "5000"} }, { {UserView, {-2.1, -2.5, 4}, ""}, {{1.3, ...


2

This isn't an answer (yet) but it was too long for a comment. Here is an extended example where the quoted precision does not appear to be true: f1 = Derivative[0, 6][StieltjesGamma][0, #] &; f2 = {Accuracy@#, Precision@#, InputForm@#} &; f1 @ 1`15 f2 @ % N[f1 @ 1, 10] f2 @ % 725.59 {2.29961, 5.1603, ...



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