Tuesday

LaTeX: Mathematical operations using pgfmath and pgffor packages

Input file name: mathematics1_pgfmath_pgffor.tex
{
\documentclass[12pt,a4paper]{article}
\usepackage{pgfmath,pgffor}

\begin{document}

1 $+$ 2 $\times$ 3 = \pgfmathparse{int(1+2*3)}\pgfmathresult

2.0 $+$ 3.0 $\times$ 4.0 = \pgfmathsetmacro{\result}{2+3*4}\result

(1.0 $+$ 2.0) $\times$ 3.0 = \pgfmathparse{(1.0+2.0)*3.0}\pgfmathresult

%r to convert from radians to degrees, pi radians is 180 degrees
sin(pi/2 radians) = \pgfmathparse{round(sin(pi/2 r))}\pgfmathresult

sin(90 degrees) = \pgfmathparse{sin(90)}\pgfmathresult

$15^{3}$ = \pgfmathparse{int(15^3)}\pgfmathresult

15 power of 3 = \pgfmathparse{int(pow(15,3))}\pgfmathresult

$\sqrt{2}$ = \pgfmathparse{sqrt(2)}\pgfmathresult

pi = \pgfmathparse{pi}\pgfmathresult

pi radians to degrees = \pgfmathparse{int(round(pi r))}\pgfmathresult

radian(180 degrees) = \pgfmathparse{rad(180)}\pgfmathresult

degree(pi radians) = \pgfmathparse{int(round(deg(pi)))}\pgfmathresult

% generate a pseudo-random number between 0 and 1
rnd(0,1) = \pgfmathparse{rnd}\pgfmathresult

% generate a pseudo-random number between -1 and 1
rand(-1,1) = \pgfmathparse{rand}\pgfmathresult

\foreach \x in {1,2,...,10} {\x \hspace{5pt}} %\linebreak[4]

\pagebreak

{
\noindent
\foreach \i in {1,2,...,30}
{
$\i^{2}$ = \pgfmathparse{int(\i^2)}\pgfmathresult \\
}
}

\pagebreak

{
\noindent
\foreach \i in {1,2,...,15}
{
$\i^{3}$ = \pgfmathparse{int(\i^3)}\pgfmathresult \\
}
}

\end{document}
}

Output:
1 + 2 × 3 = 7
2.0 + 3.0 × 4.0 = 14.0
(1.0 + 2.0) × 3.0 = 9.0
sin(pi/2 radians) = 1.0
sin(90 degrees) = 1.0
15^{3} = 3375
15 power of 3 = 3375
2 = 1.41421
pi = 3.141592654
pi radians to degrees = 180
radian(180 degrees) = 3.14159
degree(pi radians) = 180
rnd(0,1) = 0.28926
rand(-1,1) = -0.79707
1 2 3 4 5 6 7 8 9 10


1^2 = 1
2
^2 = 4
3
^2 = 9
4
^2 = 16
5
^2 = 25
6
^2 = 36
7
^2 = 49
8
^2 = 64
9
^2 = 81
10
^2 = 100
11
^2 = 121
12
^2 = 144
13
^2 = 169
14
^2 = 196
15
^2 = 225
16
^2 = 256
17
^2 = 289
18
^2 = 324
19
^2 = 361
20
^2 = 400
21
^2 = 441
22
^2 = 484
23
^2 = 529
24
^2 = 576
25
^2 = 625
26
^2 = 676
27
^2 = 729
28
^2 = 784
29
^2 = 841
30
^2 = 900

1^3 = 1
2
^3 = 8
3
^3 = 27
4
^3 = 64
5
^3 = 125
6
^3 = 216
7
^3 = 343
8
^3 = 512
9
^3 = 729
10
^3 = 1000
11
^3 = 1331
12
^3 = 1728
13
^3 = 2197
14
^3 = 2744
15
^3 = 3375

No comments:

Measure execution time with Julia, example using sorting algorithms

# random integers between 1 and 100 inclusive, generate thousands of them x = rand ( 1 : 100 , 100000 ) @time sort (x; alg=InsertionSort, r...