2

Expressions

In the previous chapter, we learnt about the fundamentals of numEclipse workbench. We looked at the numEclipse project, perspective, interpreter and how to write and execute a simple program in m-script. This chapter and the next chapter focus on the programming language. A programming language has two major parts, either it is evaluating or it is execution. The evaluation part is related with the expressions. For example, y = x + 2 is an expression where the program evaluates the value of y by using the value of x. The execution part of the program is related with statements. For example, write (“Hello World”) is a statement which prints the string “Hello World!” on the screen.

In this chapter, we will focus on different types of expressions. An expression involves variables of different type and operators. So we will look at the data types and operators applicable to those data types.

2.1 Matrix

It is the key data type of this language. There is no equivalent for this data type in standard programming languages. A matrix with only one column represents a mathematical entity known as “column vector” and a matrix with one row represents a “row vector”. A matrix is basically a two dimensional array of elements. These elements could be real numbers, complex numbers, strings, booleans, structures or cells. Multi-dimensional matrices or arrays are not supported at this point.

Listing 2.1

> > A = [1 2 3; 4 5 6; 7 8 9]

A =

1.0000 2.0000 3.0000

4.0000 5.0000 6.0000

7.0000 8.0000 9.0000

> > B = [− 9 7 4 2 − 6 4 1 3 9]

B =

− 9.0000 7.0000 4.0000

2.0000 − 6.0000 4.0000

1.0000 3.0000 9.0000

In the above listing, for the first matrix A we use semicolons to separate rows whereas in matrix B we just use a new line to indicate the start of a new row. In the above example, we used spaces to separate the elements in a row. You can also use comma to avoid any ambiguity. A single real number, complex number and even a boolean is treated as matrix of size 1 × 1.

Every variable in numEclipse is a matrix. The following example shows how to modify and access an element of a matrix.

Listing 2.2

> > A(2, 2) = 314

A =

1.0000 2.0000 3.0000

4.0000 3.1400 6.0000

7.0000 8.0000 9.0000

> > x = A(1, 3)

x =

3.0000

> > A([2 3], [1 2])

and =

4.0000 3.1400

7.0000 8.0000

An individual element of a matrix is referenced by providing the row and column index in parentheses as shown in the above example. Also note in the last example that we can also reference a sub-matrix by providing an array of indices.

2.2 Real Number

Real number is a double precision floating point number. It is equivalent to java primitive “double”. The maximum value of a real number is defined by a constant realmax, 1.7977e + 308, the minimum value is defined by another constant realmin, 4.9000e-324 and the precision of the real number is defined by eps, 2.2204e-016. The following listing shows some examples of expressions with real numbers.

Listing 2.3

> > x = 3.5

x =

3.5000

> > y = 2.7

y =

2.7000

> > z = x + y

z =

6.2000

> > realmin

realmin =

4.9000E−324

> > realmax

realmax =

1.7977E308

> > eps

eps =

2.2204E−016

2.3 Complex Number

A complex number has no equivalent in standard programming languages. It is simply a pair of real numbers. The first part is called the real part and the second part is called the imaginary part. The following listing shows some examples of complex numbers.

Listing 2.4

> > z1 = 2 + 2j

z1 = 2.0000 + 2.0000i

> > z2 = − 1 + 3i

z2 = − 1.0000 + 3.0000i

> > z3 = z1 * z2

z3 =

− 8.0000 + 4.0000i

You would notice in the above example that we use both “i” and “j” to represent the imaginary part. In the following listing, we show how to get the real and imaginary part of a complex number.

Listing 2.5

> > z = 2 – i

z =

2.0000 − 1.0000i

> > x = real(z)

x =

2.0000

> > y = imag(z)

y =

− 1.0000

numEclipse supports a number of other functions to support complex number computation, a few of them are listed in the following table 2.1.

Table 2.1

Complex functions

Function Description
conj Returns the complex conjugate
real Returns the real part
img Returns the imaginary part
abs Returns the magnitude of the complex number
angle Returns the angle of the polar representation of the complex number

2.4 Boolean

The boolean data type corresponds to the java boolean primitive type. A boolean type variable is limited to only true and false values. An expression evaluating into a number greater than zero will be treated as a true value and a zero value expression will be treated as false. In the following, we give a simple example of relational expression which evaluates to true value.

Listing 2.6

> > b = 2 > 1

b =

1.0000

> > [1 2] < 0

ans =

0.0000 0.0000

In the second example in the above listing, when we compare the array with zero value a component wise comparison is performed and 0 s are returned to represent false values. We can always use the literals true and false along with 0 s and 1 s in a boolean expression. It is demonstrated in the following example.

Listing 2.7

> > b = 2 > 1 & false

b =

0.0000

> > [1 1] & true

ans =

1.0000 1.0000

> > [0 1]|false

ans =

0.0000 1.0000

Boolean values, operators and expressions play an important role in loops and branching statements, we will shortly learn more about them.

2.5 String

The String data type is equivalent to java String. numEclipse allows both single and double quotes to enclose string literals. Unlike MATLAB, a numEclipse string is not an array of characters, it is an object. You can create an array of strings with numEclipse but it is very different from the array of characters in MATLAB. In the following, we create two string variables and we show how they could be concatenated.

Listing 2.8

> > s1 = ‘John’

s1 =

John

> > s2 = ‘Hello’

s2 =

Hello

> > s = s2 + ‘ ’ + s1

s =

Hello John

numEclipse offers a number of useful string processing functions. In the following example, we show how to compare two strings.

Listing 2.9

> > strcmp (‘John’, ‘john’)

ans =

false

> > strcmpi (‘John’, ‘john’)

ans =

true

The two functions return different truth values because strcmpi is case insensitive.

Listing 2.10

> > index (‘1234567891234567890’, ‘456’)

ans =

4.0000

> > rindex (‘12345678901234567890’, ‘456’)

ans =

14.0000

In the above example, index returns the index of first appearance of second string within first string whereas rindex returns the index of last occurrence.

2.6 Structure

Structure is a composite data type. It is very similar to struct in C language. Unlike a matrix, a structure can contain data elements of various types. Elements / fields of a structure are named rather than indexed. The individual fields are addressed using the dot notation as shown below.

structure_name > . < field_name>

A structure within an array of structures must have the same set of fields. A single structure is treated as an array of size 1 × 1. The following example shows how to create a structure.

Listing 2.11

> > employee.name = ‘ John’;

> > employee.id = 123;

> > employee.salary = 3000;

> > employee.pref = [10 20; 30 40];

> > employee

employee =

perf:

10.0000 20.0000

30.0000 40.0000

name : John

Id : 123.0000

Salary : 3000.0000

In the above example, we use the assignment to create structure fields. There is no need to declare the structure. The following example shows the use of struct function to create a structure variable.

Listing 2.12

> > emp = struct (‘name’, ‘John’, ‘id’, 123, ‘salary’,

3000)

emp =

name : John

id : 123.0000

salary : 3000.0000

This function takes an even number of arguments as a sequence of field name and value pairs.

 struct(‘name 1’, value 1, ‘name2’, value2,…)

Note that the name of a field is provided as a string. The following example shows how to access the value of a structure field.

Listing 2.13

> > emp.name = ‘John’;

> > emp.id = 123;

> > emp.salary = 3000;

> > emp.pref = [10 20; 30 40];

> > x = emp.salary

x =

3000.0000

> > s = emp.name

s =

John

Similarly, accessing the value of a field from a structure array is shown below.

Listing 2.14

> > emp (2).name = ‘Peter’;

> > emp (2)id = 456;

> > emp (2).salary = 2500;

> > emp (2).perf = [1 2; 3 4];

> > name = emp(2).name

name =

Peter

> > sal = emp(1).salary

sal =

3000.0000

> > x = emp(2).perf(2,2)

x =

4.0000

In the above example, we also show how to get a matrix element from a matrix field value of a structure array. Unlike Matlab, there are no add or remove functions available in numEclipse. You can add a field as shown in above example. There is no method available to remove a field. Using the above example, we look at the size function in the following.

Listing 2.15

> > size(emp)

ans =

1.0000 2.0000

> > size (emp.pref)

ans =

2.0000 2.0000

The first example above returns the size of the structure array emp whereas the second example returns the size of the matrix field pref within the emp structure. numEclipse does not supported nested structures at this point.

2.7 Cell

Cell array is another composite data type. It is very similar to structure array except the way it organizes the data. Unlike a structure array, each cell in an array could have an entirely different type of element. A cell array can be created using a function as shown in the following example.

Listing 2.16

> > c = cell(2,2)

> > c(1,1) = {‘Hello world’};

> > c(1,2) = {2};

> > c(2,1) = {[1 2]};

> > c(2,2) = {1 + 2i};

> > c

c =

Cell (1,1)

Hello World

Cell (1,2)

2.0000

Cell (2,1)

1.0000 2.0000

Cell (2,2)

1.0000 + 2.0000i

There is no need to use a function; a cell can be created just by using assignment statement. In the following, we show how to create a cell using curly brackets.

Listing 2.17

> > ce = {[3.13 2.71]}

ce =

3.1300 2.7100

Obtaining data from a cell element is shown in the following example.

Listing 2.18

> > x = c{2,2}

x =

1.0000 + 2.0000i

A cell array cannot be nested in another cell but it can contain structure array. Here is another example based on the cell variable “c” defined earlier.

Listing 2.19

> > c(1,2) = struct (‘name’, ‘John’, ‘id’, 123, ‘salary’, 3000)

c =

Cell(1,1)

Hello world

C(1,2)

name : John

id :123.0000

salary : 3000

Cell (2,1)

1.0000 2.0000 3.0000

Cell(2,2)

1.0000 + 2.0000i

The above examples make it clear that cell arrays are very flexible and they can hold any amount of data of any type. This feature makes them candidates for input and output variables for functions. This will be discussed in detail in the later chapters.

2.8 Range Expression

A range expression provides a short cut notation to define a vector using colon “:” operator. Say we want to define a vector containing positive integers ranging from 1 to 10, using this notation we could define it as follows.

Listing 2.20

> > m = 1:10

m =

1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000

8.0000 9.0000 10.0000

We could also define a vector from 1 to 50 with increments of 10 using two colon operators such that the number between the colons defines the increment value. It is demonstrated in the following listing.

Listing 2.21

> > n = 1:10:50

n =

1.0000 11.0000 21.0000 31.0000 41.0000

This saves us from listing all the vector elements. Similarly, we could define vectors with descending values using negative incremental steps.

Listing 2.22

> > u = 10:-2:1

u =

10.0000 8.0000 6.0000 4.0000 2.0000

The colon operator could also be used as a wildcard to select rows and columns of a matrix. When we use the colon for a subscript, it represents the entire row or column. For example, A(3, :) represents the 3rd row of matrix A and A(:, 2) represents the 2nd column.

2.9 Boolean Expression

A boolean expression is composed of boolean primitives “true” and “false” and logical operators. numEclipse supports following boolean operators.

Table 2.2

Boolean operators

Name Notation
NOT ~
AND &&, &
OR image, image

The unary NOT operator negates the truth value of a variable. The binary AND operator returns true if and only if both operands are true. The binary OR operator returns false if and only if both operands are false. Both AND and OR operators are short circuit operators which means they will not evaluate the second operand if the first is enough to evaluate the resultant. In the following, we show some examples of boolean expressions.

Listing 2.23

> > true && false

ans =

false

> > true | false

ans =

true

> > ~ true

ans =

false

> > ~ false

ans =

true

2.10 Relational Expression

A relational expression is composed of numbers (scalar, vector, matrix or complex) and relational operators. A relational expression evaluates to a boolean value. numEclipse supports following relational operators.

Table 2.3

Relational operators

Name Notation
less than <
greater than >
less than and equal to <=
greater than and equal to >=
equal to ==
not equal to <>, !=, ~=

In the following, we show some examples of relational expressions.

Listing 2.24

> > 3 > 1

ans =

true

> > A = rand(2)

A =

0.7242 0.2868

0.9793 0.9371

> > A > 0.5

ans =

1.0000 0.0000

1.0000 1.0000

It is important to note that both boolean and relational expressions evaluate to a boolean value. So they could be combined into more complex expressions as shown in the following example.

Listing 2.25

> > x = rand(2)

x =

0.82740.0648

0.45710.3825

> > y = rand(2)

y =

0.9761 0.4578

0.7462 0.6924

> > x > 0.5 && y > 0.5

ans =

1.0000 0.0000

0.0000 0.0000

2.11 Numerical Expression

A numerical expression involves variables, literal numbers and arithmetic operators. A variable or number could be a real number, complex number, vector or a matrix. numEclipse supports the arithmetic operators given in table 2.4. The operators addition, subtraction and power are obvious. numEclipse provides two types of divisions, left and right division. Left division divides the right argument and right division divides the left argument. Component-wise operators are applied on the elements of the operands. This could be explained by an example, as shown below.

Table 2.4

Arithmetic operators.

Name Notation
addition +
subtraction
multiplication *
right division /
left division
power ^
component-wise multiplication .*
component-wise right division ./
component-wise left division .
component-wise power .^

Here, when we apply dot power on matrix A, it does not square the matrix rather it squares each element of the matrix. Similar, when we dot multiply A and B, it is not a matrix multiplication rather corresponding elements of the matrices are multiplied.

Listing 2.26

> > A = [1, 2; 3, 4];

> > A.^ 2

ans =

1.0000 4.0000

9.0000 16.0000

> > B = eye(2)

B =

1.0000 0.0000

0.0000 1.0000

> > A.*B

ans =

1.0000 0.0000

0.0000 4.0000

In the following, we present a rather complicated numerical expression.

Listing 2.27

> > A

A =

0.7242 0.2868

0.9793 0.9373

> > z

z =

1.0000 – i

> > b = (A + 1) / 2−z ^ 2

b =

0.8621 + 2.0000i 0.6434 + 2.0000i

0.9896 + 2.0000i 0.9687 + 2.0000i

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset