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(true) is false
  • not(false) is true

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

  • 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 =


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


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


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


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


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 =


-->r2 = %T
r2 =


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


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


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 =


-->x & y & z
ans =


-->x & z
ans =


-->x & y
ans =


-->y & z
ans =



Otto said...

Man this tutorial of yours is really useful, i'm a master student too and this is really gonna save me.

Thank you!

Anonymous said...

Thanks and Keep up the great work!

math.man5 said...

nice tutorial...

Anonymous said...

Poda punda...........

Anonymous said...

Grt Tutorial dude...