Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.3K Views
|
16 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Decimal format in Numberform usage

Posted 2 years ago
16 Replies
Posted 2 years ago

A preamble about asking for help. I looked first in the documentation for aid in all the things I am asking here. Finding things is not straightforward if you don't know how to phrase the question. Somewhat frustrating. I have made progress, and now have some new questions as a result. I,m not sure whether or not to start a new topic, so I'll just start here and ask all the new questions. Please recommend which - if any - should be posted as new threads.

Issue 1: The numbering in the workbook is not sequential now. I had not been paying attention, but does re-evaluating an input screw up the numbering?

Issue 2: I have managed to create two columns (more seem straightforward) of displayed numbers - essentially an x-vector and a y-vector, where each y is a function of the corresponding x (I use "vector" loosely, in the sense I am used to, not necessarily the Mathematica syntax). In this case, the function is EllipticK. See in-out(142) in attached. I would like to export these numerical values to a csv file. What I want the output to look like is in the file "desired.csv" attached. I tried doing an export to csv. What I got is in the file "wmdata2.csv." See in-out(137). Clearly, M (Mathematica) doesn't treat my two-column display as a single array. I don't know how to do that, nor whether that will help create the desired output file.

Issue 3: I could not set the current working directory. See in -out(134,135, 136). The directory "D:\temp" is valid. I thought my SetDirectory syntax was correct. It didn't work. What am I missing in the syntax? what is wrong with In(135)?

Issue 4: I am counting on M to be the gold standard with arbitrary precision. Some calc examples leave me puzzled. See I-O(152,146,150) - three sequential evals with EllipticK. The input arguments in (152) and (15) are exactly the same; one is a fraction, the other a decimal. However, the output values differ after the tenth decimal place. I really need to understand what is going on here, and why the returned value is not to the requested precision. No error message/qualifier is visible.

Thx, Lou

POSTED BY: Louis Poulo
Posted 2 years ago
POSTED BY: Eric Rimbey
Posted 2 years ago
POSTED BY: Eric Rimbey
Posted 2 years ago

Issue 4: I am counting on M to be the gold standard with arbitrary precision. Some calc examples leave me puzzled. ... I really need to understand ... why the returned value is not to the requested precision.

Yep, this is a common hurdle when you start with Mathematica. If you write a decimal number with just a decimal point and no other indication about precision, it will assume default precision (something like 15 decimal digits). Wrapping such a number with N won't do anything, because N just tries to turn an expression into a number. N does allow you to specify precision (or just use the default), but it can only reduce precision, never increase it. One way to tell Mathematica what precision you want a number to be is to use the backtick:

0.9 (* this will have default precision *)
0.9`100 (* this will have 100 digits of precision *)

So, let's look at your EllipticK computation:

(* Start with infinite precision input and ask for 24 digits of precision in the output--generally the
safest way. The FullForm will show us the "true" value that Mathematica is using, not the simplified
display representation *)
N[EllipticK[49999999/50000000], 24] // FullForm
(* 10.25006118906640692521926382963816467107`24. *)

(* Start with a certain precision in your inputs and lose some during the computation. *)
EllipticK[0.99999998`24] // FullForm
(* 10.25006118906640692521926382963819144403`17.311756326608677 *)

(* Overspecify the starting precision to account for the loss in precision. *)
EllipticK[0.99999998`31] // FullForm
(* 10.25006118906640692521926382963816467854`24.311756326608673 *)

You'll probably want to start reading here: http://reference.wolfram.com/language/guide/PrecisionAndAccuracyControl.html

POSTED BY: Eric Rimbey
Posted 2 years ago
POSTED BY: Eric Rimbey
Posted 2 years ago

Thanks for the quick and clear responses. I think I have 3 of 4 items down OK. As far as directory set, I figured out that I need to use forward slash(/) in path definitions and not the backslash(), which gets interpreted as an ESC as you noted. In my case "D:/temp" works as input to Mathematica, while the std windows form is "D:\temp. I notice that the echoed return value is the std windows form.

I tried the transpose form you suggested, but it didn't work for me. See attached jpg of program and resulting csv output.

I set up a two-row numeric matrix with m1 = {{1, 2, 3}, {4, 5, 6}}.

Then using Export["test_num2.csv", m1], created the following: 1,2,3 4,5,6 as expected. Using Export["test_num2T.csv", Transpose[m1]] results in a file with 1,4 2,5 3,6 This is good. What I don't know how to do is to have a variable 'forget' its history. I tried a simple example: q=Pi qn=N[q,10] I expect qn to be a 10 decimal approximation to pi, a finite decimal and nothing more. Evaluating N[qn,10] and N[qn,20] give identical 10-digit results. qn is no longer pi, but a finite approx.Yeah! However, in the attached example, the line xn = N[Column[x/50], 20] does not seem to create a set of specific approximate decimal numbers, but seems to maintain its embedded history. How to fix so that the exported entries are as displayed in xn?

Lou

Attachment

POSTED BY: Louis Poulo
Posted 2 years ago

I'm not quite following your question. But it looks to me like you still have that Column in there somewhere.

POSTED BY: Eric Rimbey
Posted 2 years ago
POSTED BY: Eric Rimbey
Posted 2 years ago

I'm really not grokking the rest of your question, but here are some ways to "forget" a variable's value (variables don't remember any history).

x =. (* This removes the value associated to x. *)
Clear[x] (* This also removes values associated to x. *)
ClearAll[x] (* This removes values and attributes and some other things, basically makes it as if x never existed before now. *)
Remove[x] (* This removes x entirely from the environment. *)

You could also just set x to another value, whatever new value you want it to have.

POSTED BY: Eric Rimbey
Posted 2 years ago

It might be better at this point to add a new question. By starting fresh you can isolate your new question, and then maybe folks can understand better what you're asking.

POSTED BY: Eric Rimbey

Please use one of the methods explained here http://wolfr.am/READ-1ST to include your formatted code or notebook. Thank you.

POSTED BY: EDITORIAL BOARD
Posted 2 years ago

Since you're new to Mathematica, I'm going to be presumptuous and provide some guidance that you didn't ask for about something that commonly trips up newcomers.

Understand that NumberForm is just a special wrapper. Evaluate this:

NumberForm[1`20, 20]

The result will look something like this: 1.0000000000000000000. But if you look at the actual structure of the result by evaluating FullForm,

FullForm[NumberForm[1`20, 20]]

you'll see that it's

NumberForm[1.`20., 20]

NumberForm, and all of the related *Form functions are just wrappers that tell the front end how to display something. This can cause problems if you subsequently want to do calculations:

NumberForm[1`20, 20] + 1

The result displays as 1+1.0000000000000000000, because the evaluator doesn't know how to do arithmetic with NumberForm expressions, and the evaluator is looking at

Plus[1, NumberForm[1.`20., 20]]
POSTED BY: Eric Rimbey
Posted 2 years ago

Glad to have you jump in. I gather that all of the display commands should be the last things(outermost in syntax) executed, since I assume they do just that - only affect the display, and can't calculate as you point out.

POSTED BY: Louis Poulo

There are many intricacies with the usage of NumberForm and of N. According to documentation,

NumberForm works on integers as well as approximate real numbers.

The number 1/50 is neither integer nor approximate real. You must first convert it to approximate with N. Also, you cannot apply NumberForm to a whole column, but only to single numbers:

Column[Map[NumberForm[N[#, 20], 20] &, {1, 2, 4, 6, 7, 49.999999}/50]]
POSTED BY: Gianluca Gorni
Posted 2 years ago

I haven't tried your composite line as yet; presumably it will give the column I'm looking for. I'll study help on and parse the sequence of commands in this. I haven't seen Map as yet; so a lot to learn.

Thx

POSTED BY: Louis Poulo

This version may be easier to parse:

Column[{1, 2, 4, 6, 7, 49.999999}/50]
N[%, 30]
NumberForm[%, 20]

First you see the numbers as you typed them. Then you see what happens by calculating all exact numbers with 30 digits of precision but default display. Last we see all numbers that have at least 20 digits of precition displayed with 20 digits.

The matter is complicated. Don't get discouraged if you feel confused at the start.

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard