Difference between revisions of "A few basic examples"

From Remeis-Wiki
Jump to navigation Jump to search
 
(11 intermediate revisions by the same user not shown)
Line 14: Line 14:
 
You can get more information on any command by starting isis (just type <code>isis</code> in the console) and entering <code>help commandname</code>, e.g., <code>help print</code>.
 
You can get more information on any command by starting isis (just type <code>isis</code> in the console) and entering <code>help commandname</code>, e.g., <code>help print</code>.
  
Now that we said hello to the world, we'll address some maths first.
+
Now that we said hello to the world, we'll address some simple way to deal with variables first.
 +
 
 +
 
 +
== Variables & Co. ==
 +
 
  
 
Define a variable x that equals 7.5:
 
Define a variable x that equals 7.5:
Line 27: Line 31:
  
 
creates a variable y and assigns it to the string "hello".
 
creates a variable y and assigns it to the string "hello".
 
  
 
You can also define a variable and make it equal something in one go:
 
You can also define a variable and make it equal something in one go:
Line 33: Line 36:
 
  variable x = 7.5;
 
  variable x = 7.5;
 
  variable y = "hello";
 
  variable y = "hello";
 +
 +
Let's create a double array of length 12 and an string type array of length 12:
 +
 +
variable dd_ex = Double_Type[12];
 +
variable ss_ex = String_Type[12];
 +
 +
If you print both arrays, you will see that <code>dd_ex</code> has entries that are all set to 0.0 and <code>ss_ex</code> entries that are all set to empty strings.
 +
 +
 +
We can also create an array by directly assigning values to it's entries:
 +
 +
variable dd = [12.5,16.7,8.88];
 +
 +
Or we can create an integer array with values from 0 to 11:
 +
 +
variable ii = [0:11];
 +
 +
 +
ISIS/s-lang are clever enough to know how to do mathematical operations (+,-,*,/,^) on arrays if they are of the same length. A mathematical operation between an array and a simple number means that the same operation will be applied to all entries in the array. For example we can set all entries to <code>dd_ex</code> to the square root of <code>ii</code> plus 1.
 +
 +
dd_ex = sqrt(ii)+1;
 +
 +
== Arrays ==
 +
 +
=== Array indexing ===
 +
 +
ISIS/s-lang start counting array entries with zero. I.e., the first entry in an array has the index 0, the second 1, etc. If we want to print the third entry in the array <code>dd</code>, we have to write:
 +
 +
print(dd[2]);
 +
 +
We can set the second entry in <code>dd</code> to 10.5 by writing:
 +
 +
dd[1] = 10.5;
 +
 +
If we want to set dd equal to the first three entries of dd_ex, we have to write:
 +
 +
variable idx = [0,1,2];
 +
dd = dd_ex[idx];
 +
 +
A very useful function is <code>where</code> which returns the indices of array entries that fullfill certain conditions. For example, if we want to know which entries in the array <code>dd_ex</code> are larger than 3, we can write:
 +
 +
variable idx2 = where(dd_ex > 3);
 +
 +
=== A few other useful array functions ===
 +
 +
Finding out the length of an array and saving it into the variable <code>nn</code>:
 +
 +
variable nn = length(dd_ex)
 +
 +
 +
== Loops and conditional statemens ==
 +
 +
=== For-loop ===

Latest revision as of 08:57, 22 January 2020


This page is intended to give a few examples for students (mainly students working with Victoria, but the rest is welcome to use it!), who have little to no coding background.

A good convention is to always use filenames that end in .sl for your scripts. You can then run the script using isis filename.sl from the console. Always end your script with exit; so that it exists once it has finished running.

Let's write a simple scripts that says "hello world"!

print("hello world");
exit;

You can get more information on any command by starting isis (just type isis in the console) and entering help commandname, e.g., help print.

Now that we said hello to the world, we'll address some simple way to deal with variables first.


Variables & Co.

Define a variable x that equals 7.5:

variable x;
x = 7.5;

Above, the first line defines an empty variable that does not yet have a type. The second line assigns this variable a value, 7.5, and automatically assigns it a corresponding type. In this case a double. A variable does not have to be a number:

variable y;
y = "hello";

creates a variable y and assigns it to the string "hello".

You can also define a variable and make it equal something in one go:

variable x = 7.5;
variable y = "hello";

Let's create a double array of length 12 and an string type array of length 12:

variable dd_ex = Double_Type[12];
variable ss_ex = String_Type[12];

If you print both arrays, you will see that dd_ex has entries that are all set to 0.0 and ss_ex entries that are all set to empty strings.


We can also create an array by directly assigning values to it's entries:

variable dd = [12.5,16.7,8.88];

Or we can create an integer array with values from 0 to 11:

variable ii = [0:11];


ISIS/s-lang are clever enough to know how to do mathematical operations (+,-,*,/,^) on arrays if they are of the same length. A mathematical operation between an array and a simple number means that the same operation will be applied to all entries in the array. For example we can set all entries to dd_ex to the square root of ii plus 1.

dd_ex = sqrt(ii)+1;

Arrays

Array indexing

ISIS/s-lang start counting array entries with zero. I.e., the first entry in an array has the index 0, the second 1, etc. If we want to print the third entry in the array dd, we have to write:

print(dd[2]);

We can set the second entry in dd to 10.5 by writing:

dd[1] = 10.5;

If we want to set dd equal to the first three entries of dd_ex, we have to write:

variable idx = [0,1,2];
dd = dd_ex[idx];

A very useful function is where which returns the indices of array entries that fullfill certain conditions. For example, if we want to know which entries in the array dd_ex are larger than 3, we can write:

variable idx2 = where(dd_ex > 3);

A few other useful array functions

Finding out the length of an array and saving it into the variable nn:

variable nn = length(dd_ex)


Loops and conditional statemens

For-loop