# Basic Statistics and Data Analysis

## Creating Matrices in Mathematica

A matrix is an array of numbers arranged in rows and columns. In Mathematica matrices are expressed as a list of rows, each of which is a list itself. It means a matrix is a list of lists. If a matrix has n rows and m columns then we call it an n by m matrix. The value(s) in the ith row and jth column is called the i, j entry.

In mathematica, matrices can be entered with the { } notation, constructed from a formula or imported from a data file. There are also commands for creating diagonal matrices, constant matrices and other special matrix types.

## Creating matrices in Mathematica

• Create a matrix using { } notation
mat={{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
but output will not be in matrix form, to get in matrix form use command like
mat//MatrixForm
• Creating matrix using Table command
mat1=Table[b{row, column},
{row, 1, 4, 1}, {column, 1, 2, 1}]
];
MatrixForm[mat1]
• Creating symbolic matrix such as
mat2=Table[xi+xj , {i, 1, 4}, {j, 1, 3}]
mat2//MatrixForm
• Creating a diagonal matrix with nonzero entries at its diagonal
DiagonalMatrix[{1, 2, 3, r}]//MatrixForm
• Creating a matrix with same entries i.e. a constant matrix
ConstantArray[3, {2, 4}]//MatrixForm
• Creating an identity matrix of order n × n
IdentityMatrix[4]

## Matrix Operations in Mathematica

In mathematica matrix operations can be performed on both numeric and symbolic matrices.

• To find the determinant of a matrix
Det[mat]
• To find the transpose of a matrix
Transpose[mat]
• To find the inverse of a matrix for linear system
Inverse[mat]
• To find the Trace of a matrix i.e. sum of diagonal elements in a matrix
Tr[mat]
• To find Eigenvalues of a matrix
Eigenvalues[mat]
• To find Eigenvector of a matrix
Eigenvector[mat]
• To find both Eigenvalues and Eigenvectors together
Eigensystem[mat]

Note that +, *, ^ operators all automatically work element-wise.

## Displaying matrix and its elements

• mat[[1]]         displays the first row of a matrix where mat is a matrix create above
• mat[[1, 2]]     displays the element from first row and second column, i.e. m12 element of the matrix
• mat[[All, 2]]  displays the 2nd column of matrix

References

## Using Built in Functions in Mathematica

There are thousands of thousands of built in functions in mathematica. Knowing a few dozen of the more important will help to do lots of neat calculations. Memorizing the names of the most of the functions is not too hard as approximately all of the built in functions in mathematica follow naming convention (i.e. name of functions are related to objective of their functionality), for example, Abs function is for absolute value, Cos function is for Cosine and Sqrt is for square root of a number. The important thing than memorizing  the function names is remembering the syntax needed to use built-in function. Remembering many of built in (built-in) mathematica functions will not only make it easier to follow programs but also enhance own programming skills too.

### Some important and widely used built in functions in Mathematica are

• Sqrt[ ]:   used to find the square root of a number
• N[ ]:   used for numerical evaluation of any mathematical expression e.g. N[Sqrt[27]]
• Log[  ]: used to find the log base 10 of a number
• Sin[  ]: used to find trigonometric function Sin
• Abs[  ]: used to find the absolute value of a number

Common built in functions in Mathematica includes

1. Trignometric functions and their inverses
2. Hyperbolic functions and their inverses
3. logarithm and exponential functions

Every built-in function in Mathematica has two very important features

• All built-in function in methematica begins with Capital letters, such as for square root we use Sqrt, for inverse cosine we use ArCos built-in function.
• Square brackets are always used to surround the input or argument of a function.

For computing absolute value -12, write on command prompt Abs[-12]  instead of for example Abs(-12) or Abs{-12} etc i.e.   Abs[-12] is valid command for computing absolute value of -12.

Note that:

In mathematica single square brackets are used for input in a function, double square brackets [[ and ]] are used for lists and parenthesis ( and ) are used to group terms in algebraic expression while curly brackets { and } are used to delimit lists. The three sets of delimiters [ ], ( ), { } are used for functions, algebraic expression and list respectively.

## Introduction to Mathematica

MATHEMATICA originally created by Steven Wolfram, a product of Wolfram Research, Inc. Mathematica is available for different operating systems, such as SGI, Sun, NeXT, Mac, DOS, and Windows. This introduction to Mathematica will help you to understand its use as mathematical and programming language with numerical, symbolic and graphical calculations.

## Mathematica can be used as:

1. A calculator for arithmetic, symbolic and algebraic calculations
2. A language for developing transformation rules, so that general mathematical relationships can expressed
3. An interactive environment for exploration of numerical, symbolic and graphical calculations
4. A tool for preparing input to other programs, or to process output from other programs

## Getting Started

Starting Mathematica will open a fresh window or a notebook, where we do all mathematical calculations and do some graphics. Initially windows title is “untitled-1” which can be changed after saving the notebook by name as desired. Mathematica notebook with text, graphics, and Mathematica input and output

## Entering Expressions

Type 1+1 in notebook and press ENTER key from keyboard. You will get answer on the next line of work area. This is called evaluating or entering the expression. Note that Mathematica places “In[1]:=” and “out[1]=” (without quotation marks) labels to 1+1 and 2 respectively. You will also see set of brackets on the right side of input and output. The inner most brackets enclose the input and output while the outer bracket (larger bracket) groups the input and output together. Each bracket contains a cell. Each time you enter or change the input you will notice that the “In” and “Out” labels will also be changed.

## Basic Artihmetic

Mathematica can perform basic operation of additions (+) , subtraction (-), multiplication (*), division (/), exponentiation(^) etc. For example write the following line for basic arithmetic in Mathematica

2*3+4^2
5*6
2(3+4)
(2-3+1)(1+2/3)-5^(-1)
6!

## Using Previous Results in Mathematica

Often we need the output of first (previous) calculations in our next (coming) computation. For this purpose % symbol can be used to refer to the output of the previous cell. For example,

2^5
% + 100

Here 2^5 is added in 100.

%% refers to the result before the last results (2nd last).

## Exact vs Approximation

Mathematica can gave approximate results; when we need

3^20/2^21 produces $\frac{3486784401}{2097152}$

We can force Mathematica to approximate result in decimal by putting decimal in expression (with any digit or number) such as

3.0^20/ 2^21

For a decimal in number in an expression, Mathematica consider it to be an approximation rather than an exact number.