Export data with a given display precision

Hello,

I learned in this forum that if one wants to display the elements of a matrix up to 6 decimal figures one uses

interface(displayprecision=6);

But then I tried to export a matrix with ctrl+c / ctrl+v to another application and when I pasted it the matrix appeared with a different number of decimal figures. My question is: how do I export a matrix with a given number of decimal figures?

Thanks,
Bernardo

John Fredsted's picture

Use only evalf

There is no need to involve the displayprecision. Just use evalf as illustrated by the following example:

Matrix(3,3,(i,j) -> evalf(sin(i*j/9),6));

When you copy-paste the output from this example to another application the number of decimal figures come out correctly.

Note that the displayprecision only controls with how many figures floating point numbers should be displayed, not how many figures they are actually calculated to.

I got it

Dear John,

that is exactly what I wanted to know. I did not know the evalf command could be used in this way.

Many thanks for that.
Regards,
Bernardo

gulliet's picture

Digits:=6

One possible way is to use the Digits environment variable.

    |\^/|     Maple 11 (IBM INTEL NT)
._|\|   |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2007
 \  MAPLE  /  All rights reserved. Maple is a trademark of
 <____ ____>  Waterloo Maple Inc.
      |       Type ? for help.
> restart;
> help("Digits");
The Digits Environment Variable

Calling Sequence
     Digits := n


Parameters
     n - natural number


Description
- The Digits environment variable controls the number of digits that Maple
  uses when calculating with software floating-point numbers.

- The default value of Digits is 10. The value of Digits is changed by using
  the assignment operator.

- Maple fully evaluates environment variables such as Digits in all contexts.
  Because Digits is an environment variable, any assignments to it inside a
  procedure body are undone upon exit from the procedure.

- The maximum value of Digits is obtained from kernelopts(maxdigits).


Examples
> Digits := 20;

                                 Digits := 20

> f := proc()
>         print( "Entering f. Digits is", Digits );
>         Digits := Digits + 17;
>         print( "Adding 17 to Digits yields", Digits );
> end proc:
> f();

                         "Entering f. Digits is", 20

                       "Adding 17 to Digits yields", 37

> Digits;

                                      20

  When returning from f in the procedure above, Maple resets Digits to its
# value on entry.

> 1.02^10;

                            1.2189944199947571302

> Digits := 5;

                                 Digits := 5

> 1.02^10;

                                    1.2190


See Also
assignment, environmentvariables, evalf, Maple_floats, float, type[float]

> Digits := 6;
                                  Digits := 6

> M := evalf(Matrix([[cos((1/3)*Pi), sin\
> ((1/3)*Pi)], [-sin((1/3)*Pi), cos((1/3)*Pi)]]));
                              [0.500000     0.866025]
                         M := [                     ]
                              [-0.866025    0.500000]

>                            Matrix(%id = 163704560)

Regards,
--
Jean-Marc

Robert Israel's picture

Exporting data

First use

> evalf(yourmatrix,6);

to create a version of your matrix that actually has 6-digit entries (rather than just displaying 6 digits), then export that one.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}