Message Boards Message Boards

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

Decimal format in Numberform usage

Posted 1 year ago

I am a new user, trying to learn the basics of Mathematica.

I defined x as x = {1, 2, 4, 6, 7, 49.999999} When I try to use NumberForm to give a specified # of decimal places, it doesn't do what expect (display each result to the specified # of decimals),the function according to the documentation. See attached example 1.

Out[78] shows the input as a column. This is as expected. I expect Out[82] to have decimal format for all values of x. It doesn't. What am i missing in the usage of NumberForm?

In example 2, only one value in Out[69] is shown with decimal value. Why? Thx, LP

Attachment

Attachment

POSTED BY: Louis Poulo
16 Replies
Posted 1 year 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 1 year ago

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?

If you're referring to the In[] and Out[] labels, yes. Those labels show you the order in which cells were evaluated, not the order in which they appear in the notebook. If you were to evaluate one cell 10 times in a row, then that cell's label would be incremented 10 times.

POSTED BY: Eric Rimbey
Posted 1 year ago

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)?

Looks to me like you need to escape the \. If you look at the message, it looks like the \t was interpreted as a tab. You probably need something like "D:\temp". You can also use FileNameJoin to avoid dealing with system-dependent delimiters.

POSTED BY: Eric Rimbey
Posted 1 year 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 1 year ago

Issue 2: I have managed to create two columns (more seem straightforward) of displayed numbers... I would like to export these numerical values to a csv file...

The problem here is that Column. That's another display wrapper, and you don't want it to be exported. In general, as you're doing your computations, keep the data in its "native" form and only use the display forms for dispaly--don't let them leak into the actual data. So, let's try again:

x = {1, 2, 4, 6, 7, 49.999999}; (* Good! Simple data. *)
xn = N[x/50, 20]; (* Still just simple data. Note the precision of the last value won't have changed, however. *)
Column[xn] (* Now you can look at what you have. TableForm is probably the better choice for display, however. *)

yn = Map[EllipticK, xn]; (* This will be a list of the results. We can't do much about the precision of the output, 
since we've already set the precision of our inputs. Refer to my other post to address that aspect. *)

Transpose[{xn, yn}] // TableForm (* Looking at what we have now. The Transpose gets them paired properly. 
We don't really need to store this in a variable just to look at it, but we could have done that if we wanted.*)

Export[FileNameJoin[{$HomeDirectory, "test.csv"}], Transpose[{xn, yn}]]
(* This should get you a well-formated csv in your home directory. You could change the format of the numbers before you export if you wanted to. *)
POSTED BY: Eric Rimbey
Posted 1 year 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 1 year 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 1 year ago

So, just don't do this:

xn = N[Column[x/50], 20]

There's no reason to set a variable to a Column expression. I mean, I suppose there could be a case where you're building up a visual display and so you want to have a variable to represent part of that display so you can refer to it, but to me it looks like you're trying to compute some data, and in that case the Column is just going to make life difficult.

If you like looking at your data at certain points during the calculation, then do this:

xn = N[x/50, 20];
Column[xn]

Your variable xn will be set to something useful, and you'll have a nice display of it to look at.

POSTED BY: Eric Rimbey
Posted 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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

Group Abstract Group Abstract