Friday, February 27, 2009

Logic Operations

I think the most one knows something about logic operations, but let's do a little review.

Logic operations are used for analysis of situations like:
  • 2 is bigger than 3? - false
  • the vector [1 2 3 4] is larger than [1 3 5]? - true
  • the word 'horse' is smaller than 'dog'? - false

The logic operations combine two or more situations, for example:

I'm 22, my father's 52 and my sister's 19.

The question: who is the older?

The solution:
  • if I'm older than my father and my sister then I'm the older.
  • if my father's older than me and my sister then he's the older.
  • if my sister's older than my father and me then she's the older.
The and is a logic operator.

The logic operators are used with logic variables. Logic variables are like numeric variables but the values are true or false.

The basic logic operators are: {not, or, and}

The not is a unary operator and the others are binary operators.

So, follow the possible logic situations for each operation:

NOT
  • not(true) is false
  • not(false) is true

AND
  • false and false is false
  • false and true is false
  • true and false is false
  • true and true is true

OR
  • false or false is false
  • false or true is true
  • true or false is true
  • true or true is true

A mental train.

If I want a racket and a ball but I have just a racket I'm not satisfied. But, If I want a ball of basketball or a ball of soccer, so I need one ball or both.


The Scilab has that three operators.

The operator '~' is the not.

The operator '|' is the or.

The operator '&' is the and.

The operations or and and can be applied on vectors with the functions or(.) and and(.).

Let's play now.

-->x = 10;

-->y = 15;

-->z = x + y
z =

25.

-->(x > y) & (z > x) // x is smaller than y then the first operation is false
ans =

F

-->(x > y) | (z > x) // z is bigger than x then the second operation is true
ans =

T

-->~((x > y) & (z > x)) // the result is the inverse of the first result
ans =

T

-->~((x > y) | (z > x)) // the result is the inverse of the second result
ans =

F


The results are F (false) or T (true) because logic operations give logic results.

If you want use the variables with logic values:

-->r1 = %F
r1 =

F

-->r2 = %T
r2 =

T

-->and([r1 r2])
ans =

F

-->or([r1 r2])
ans =

T


For finish the post, if you use a numeric variable with logic operators, thus the Scilab interprets the 0 (zero) as false and different of 0 (zero) as true.

-->x = 0;

-->y = 2;

-->z = -5;

-->x | y | z
ans =

T

-->x & y & z
ans =

F

-->x & z
ans =

F

-->x & y
ans =

F

-->y & z
ans =

T

Wednesday, February 11, 2009

Vectors and Matrices - 3

Now, I'll write about specific functions for matrices.

The most common functions that I use:

  • eye(m,n) - identity matrix;
  • zeros(m,n) - matrix of zeros;
  • ones(m,n) - matrix of ones;
  • det(X) - determinant;
  • inv(X) - matrix inverse;
  • pinv(A,[tol]) - pseudoinverse;
  • sum(x,[key]) - sum (row sum, column sum) of vector/matrix entries;
  • prod(x,[key]) - product (row sum, column sum) of vector/matrix entries;
  • mean(x,[key]) - mean (row sum, column sum) of vector/matrix entries;
  • stdev(x,[key]) - standard deviation (row sum, column sum) of vector/matrix entries;
  • geomean(x,[key]) - geometric mean (row sum, column sum) of vector/matrix entries;
  • harmean(x,[key]) - harmonic mean (row sum, column sum) of vector/matrix entries;
  • msd(x,[key]) - mean squared deviation (row sum, column sum) of vector/matrix entries;
  • rand(m1,m2,.. [,key]) - random number generator;
  • grand(m, n, dist_type [,p1,...,pk]) - Random number generator(s);
  • find(X) - find indices of boolean vector or matrix true elements.
Each function has it own purpose.

Let's play, with them.

-->eye(3,3) // identity matrix 3 lines and 3 columns*
ans =

1. 0. 0.
0. 1. 0.
0. 0. 1.

-->rand(5,2) // matrix (5 lines and 2 columns) of random numbers with uniform probability distribution between [0,1]
ans =

0.0683740 0.5442573
0.5608486 0.2320748
0.6623569 0.2312237
0.7263507 0.2164633
0.1985144 0.8833888

-->rand(2,5,'normal') // matrix (2 lines and 5 columns) of random numbers with normal (Gaussian) distribution (mean = 0 and variance = 1)
ans =

1.0478272 - 1.4061926 - 1.7350313 - 0.2143931 2.5891773
- 1.3218008 - 1.0384734 0.5546874 - 2.0735088 0.0424792

-->det(eye(4,4)) // determinant of identity matrix 4 x 4
ans =

1.

-->X = rand(5,5) // X is a matrix (5 x 5) with random values between [0,1]
X =

0.4368588 0.0437334 0.1280058 0.1531217 0.8784126
0.2693125 0.4818509 0.7783129 0.6970851 0.1138360
0.6325745 0.2639556 0.2119030 0.8415518 0.1998338
0.4051954 0.4148104 0.1121355 0.4062025 0.5618661
0.9184708 0.2806498 0.6856896 0.4094825 0.5896177

-->det_X = det(X) // det_X is the determinant of X
det_X =

- 0.0799884

-->[py px] = find(eye(3,3)) // px is the positions (x-coordinates) of numbers nonzeros and py is the positions (y-coordinates) of numbers nonzeros
px =

1. 2. 3.
py =

1. 2. 3.

-->[py px] = find(~eye(3,3)) // the ~ is the logic negative operation, so (~0) = 1 and (~1) = 0**
px =

1. 1. 2. 2. 3. 3.
py =

2. 3. 1. 3. 1. 2.


-->X // the variable X (X = rand(5,5))
X =

0.4368588 0.0437334 0.1280058 0.1531217 0.8784126
0.2693125 0.4818509 0.7783129 0.6970851 0.1138360
0.6325745 0.2639556 0.2119030 0.8415518 0.1998338
0.4051954 0.4148104 0.1121355 0.4062025 0.5618661
0.9184708 0.2806498 0.6856896 0.4094825 0.5896177

-->sum(X,'r') // sum over the columns of X (row of values)
ans =

2.6624119 1.4850001 1.9160468 2.5074436 2.3435661

-->sum(X,'c') // sum over the lines of X (column of values)
ans =

1.6401323
2.3403973
2.1498187
1.9002098
2.8839105

-->sum(X) // sum of all elements of X
ans =

10.914469


So, if anyone has any question, then everyone can do comments.

----------

* If you do a command without send the return to a variable, then the Scilab creates the variable ans (answer) with the return or function or operation.

For example:

-->1+1
ans =

2.

-->cos(%pi)
ans =

- 1.

-->sin(%pi/2)+1
ans =

2.



** I'll write about logic operations in next posts.

Saturday, February 7, 2009

Vectors and Matrices - 2

In the last post, I wrote about simple operations with matrices. Now, I want to talk about the functions and we will use them on matrices and vectors.

All right, let's start. The Scilab has many math functions, like sin, cos, tan, asin, acos, atan, exp, log, etc....

All functions can be used on any type of numeric variable (scalar, vector, matrix or tensor).

First example:
-->x = [1 0 1;
-->0 1 0;
-->1 0 1]
x =

1. 0. 1.
0. 1. 0.
1. 0. 1.

-->y = cos(%pi*x) // the Scilab has some common values, I know PI (%pi = 3.1415927) and E (%e = 2.7182818)
y =

- 1. 1. - 1.
1. - 1. 1.
- 1. 1. - 1.


Look that cos(%pi) = -1 and cos(0) = 1 (remember the trigonometric circle).

So, operations with matrices create new matrices, and the new matrices can be used on the new operations or functions.

Let's go to the second example.
-->x = [1 2 3]
x =

1. 2. 3.

-->y = [3;
-->2;
-->1]
y =

3.
2.
1.

-->z1 = exp(x*y)
z1 =

22026.466

-->z2 = exp(y*x)
z2 =

20.085537 403.42879 8103.0839
7.3890561 54.59815 403.42879
2.7182818 7.3890561 20.085537


The operation x*y results in
x*y = [1*3 + 2*2 + 3*1] = [3 + 4 + 3] = 10.
The operation y*x results in
y*x =
[3*1 3*2 3*3;
2*1 2*2 2*3;
1*1 1*2 1*3] =
[3 6 9;
2 4 6;
1 2 3].

The function exp(.) returns the natural exponential (exp(x) = %e^x).

Ok, let's finish here. Who wants more, do comments.