Difference between revisions of "OpenModelica/C2/Array-Functions-and-Operations/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "{| border=1 ||'''Visual Cue''' ||'''Narration''' |- || Slide: '''Title Slide''' || Welcome to the Spoken Tutorial on '''Array Functions and Operations'''. |- || Slide: '''...")
 
Line 16: Line 16:
  
 
* how to use '''OMShell'''
 
* how to use '''OMShell'''
 
 
* how to use '''array construction functions'''.
 
* how to use '''array construction functions'''.
 
* how to perform '''arithmetic operations''' on '''vectors''' and '''matrices'''.
 
* how to perform '''arithmetic operations''' on '''vectors''' and '''matrices'''.
* how to use '''conversion functions'''.
+
* how to use '''array conversion functions'''.
  
 
|-
 
|-
Line 41: Line 40:
 
* knowledge of '''function''' and '''array declaration''' in '''Modelica'''.
 
* knowledge of '''function''' and '''array declaration''' in '''Modelica'''.
 
* Prerequisite tutorials are mentioned on our website.
 
* Prerequisite tutorials are mentioned on our website.
 +
* Please go through them.
  
 
|-
 
|-
Line 46: Line 46:
  
 
'''OMShell'''
 
'''OMShell'''
|| '''OMShell''' is an interactive command line tool. It is a part of '''OpenModelica'''.
+
|| let us learn more about '''OMShell''' now.
 +
 
 +
'''OMShell''' is an interactive command line tool.  
 +
 
 +
It is a part of '''OpenModelica'''.
  
 
'''OpenModelica compiler''' can be invoked using '''commands''' typed in '''OMShell'''.
 
'''OpenModelica compiler''' can be invoked using '''commands''' typed in '''OMShell'''.
Line 56: Line 60:
 
|-
 
|-
 
||  
 
||  
|| We shall now use '''classes''' named '''polynomialEvaluatorUsingVectors''' and '''functionTester''' to demonstrate OMShell.  
+
|| We shall now use '''classes''' named '''polynomialEvaluatorUsingVectors''' and '''functionTester''' to demonstrate '''OMShell'''.  
  
These '''classes''' were discussed in the previous tutorials.
+
These '''classes''' were discussed in previous tutorials.
  
 
For more information on these '''classes''', please watch the prerequisite tutorials.
 
For more information on these '''classes''', please watch the prerequisite tutorials.
Line 70: Line 74:
 
|-
 
|-
 
|| Click on '''Search''' icon in '''Icon Tray'''.
 
|| Click on '''Search''' icon in '''Icon Tray'''.
|| Let me launch '''OMShell'''.  
+
|| Now let me launch '''OMShell'''.  
  
 
To open '''OMShell''' on '''Ubuntu''' Operating System,  
 
To open '''OMShell''' on '''Ubuntu''' Operating System,  
Line 82: Line 86:
 
Click on '''OMShell''' icon.  
 
Click on '''OMShell''' icon.  
  
In '''Windows''', you may find it in '''Start''' menu.
+
In '''Windows''', you may find the icon in '''Start''' menu.
  
 
|-
 
|-
 
|| /* OMShell */
 
|| /* OMShell */
|| '''OMShell''' has now opened.  
+
|| Now let us learn a few useful commands.
  
Let me resize the window for better visibility.
+
Firstly, go to the location where you saved the text file named '''OMShell-commands.txt''' and open it.  
 
+
Let us learn a few useful '''commands'''.
+
 
+
|-
+
||
+
|| Now, go to the location where you saved the text file named '''OMShell-commands.txt''' and open it.  
+
  
 
Note that this file has all the '''commands''' to be used in this tutorial.  
 
Note that this file has all the '''commands''' to be used in this tutorial.  
Line 100: Line 98:
 
Hence, you may refer to this file whenever in doubt.  
 
Hence, you may refer to this file whenever in doubt.  
  
Let me switch to '''OMshell'''.
+
Now let me switch to '''OMShell'''.
  
 
|-
 
|-
Line 114: Line 112:
 
|-
 
|-
 
|| Type // '''cd(“path”)''' //
 
|| Type // '''cd(“path”)''' //
|| Change current directory to the location where you have saved the '''code files'''.
+
|| Now let us change current directory to the location where you have saved the '''code files'''.
  
 
Let me change directory on my system.
 
Let me change directory on my system.
  
Type '''cd(open and close parentheses) (within double quotes)''' .
+
Type '''cd'''(open and close parentheses) (within double quotes), specify the path.
  
Specify the path and press '''Enter'''.
+
Press '''Enter'''.
  
 
|-
 
|-
 
||  
 
||  
|| Note that a '''Windows''' path uses '''forward slash''' unlike the '''backward slash''' in '''Ubuntu'''.  
+
|| Note that a '''Windows''' path uses '''forward slash''' unlike the '''backward slash''' used in '''Ubuntu'''.  
  
 
'''Windows''' users need to be cautious of this fact.
 
'''Windows''' users need to be cautious of this fact.
Line 130: Line 128:
 
|-
 
|-
 
|| Type // '''loadFile(“polynomialEvaluatorUsingVectors.mo”) '''//
 
|| Type // '''loadFile(“polynomialEvaluatorUsingVectors.mo”) '''//
|| Let us load '''polynomialEvaluatorUsingVectors''' function.
+
|| Now let us load '''polynomialEvaluatorUsingVectors''' function.
  
Type '''loadFile (within parentheses) (within double quotes)'''.
+
Type '''loadFile''' (within parentheses) (within double quotes) '''polynomialEvaluatorUsingVectors.mo'''.
 
+
Type '''polynomialEvaluatorUsingVectors.mo'''.
+
  
 
Note that '''F''' is upper-case in '''loadFile()''' command.
 
Note that '''F''' is upper-case in '''loadFile()''' command.
  
'''loadFile''' command can be used to load '''class''' or '''model''' files with a file extension of '''.mo''' and press '''Enter'''.
+
This command can be used to load '''class''' or '''model''' files with a file extension of '''.mo'''.
 +
 
 +
Now press '''Enter'''.
  
 
|-
 
|-
Line 146: Line 144:
 
|-
 
|-
 
|| Type // '''polynomialEvaluatorUsingVectors(10) //'''
 
|| Type // '''polynomialEvaluatorUsingVectors(10) //'''
|| Let us call this function interactively.
+
|| Now let us call this function interactively.
  
Type '''polynomialEvaluatorUsingVectors (with an argument of) 10'''.
+
Type '''polynomialEvaluatorUsingVectors''' (with an argument of) '''10'''.
  
 
Press '''Enter'''.
 
Press '''Enter'''.
Line 158: Line 156:
 
|-
 
|-
 
|| Type // '''loadFile(“functionTester.mo”) '''//
 
|| Type // '''loadFile(“functionTester.mo”) '''//
|| Let me load '''functionTester''' class.
+
|| Let me now load '''functionTester''' class.
  
Type '''loadFile(functionTester.mo)'''
+
Type '''loadFile''' (open and close parentheses) (within double quotes) '''functionTester.mo'''.
 +
 
 +
Press '''Enter'''.
  
 
|-
 
|-
 
|| Type // '''simulate(functionTester,startTime=0,stopTime=1) '''//
 
|| Type // '''simulate(functionTester,startTime=0,stopTime=1) '''//
|| Let us simulate '''functionTester'''.
+
|| Now let us simulate '''functionTester''' class.
  
Type '''simulate (within brackets) functionTester (comma) startTime (equals) 0 (comma) stopTime (equals) 1''' and press '''Enter'''.
+
Type '''simulate''' (within parentheses) '''functionTester''' (comma) '''startTime''' (equals) '''0 stopTime''' (equals) '''1'''.
 +
 
 +
Press '''Enter'''.
  
 
|-
 
|-
 
||  
 
||  
|| The simulation is complete.
+
|| The simulation is now complete.
  
Let us plot variable '''z''' from '''functionTester class.
+
Let us plot variable '''z''' from '''functionTester''' class.
  
 
|-
 
|-
 
|| Type // '''plot({z}) '''//
 
|| Type // '''plot({z}) '''//
|| Type '''plot (within parentheses) (within curly braces) z''' and press '''Enter'''.  
+
|| Type '''plot''' (within parentheses) (within curly braces) '''z''' and press '''Enter'''.  
  
This generates a plot of variable '''z''' vs '''time'''.
+
This command generates a plot of variable '''z''' vs '''time'''.
  
 
|-
 
|-
 
||  
 
||  
|| Let me go back to the slides.
+
|| Now let me go back to the slides.
  
 
|-
 
|-
Line 199: Line 201:
 
We will also practice them using '''OMShell'''.
 
We will also practice them using '''OMShell'''.
  
'''fill()''' is used to construct '''arrays''' with all the elements same.
+
'''fill()''' is function used to create an '''array''' with all the elements same.
  
 
The syntax for '''fill''' is as shown.  
 
The syntax for '''fill''' is as shown.  
Line 205: Line 207:
 
First '''argument''' represents the number which fills the '''array'''.  
 
First '''argument''' represents the number which fills the '''array'''.  
  
Remaining '''arguments''' represent the size of each dimension of the array.
+
Remaining '''arguments''' represent the size of each dimension.
  
'''zeros()''' function is used to create an '''array''' filled with zeros.
+
'''zeros()''' is a function used to create an '''array''' filled with zeros.
  
 
Syntax for '''zeros()''' function is as shown.  
 
Syntax for '''zeros()''' function is as shown.  
  
'''Arguments''' represent the size of each dimension.
+
'''Arguments''' represent the size of each dimension of the array.
  
 
'''identity()''' function creates an identity matrix.  
 
'''identity()''' function creates an identity matrix.  
Line 219: Line 221:
 
|-
 
|-
 
||  
 
||  
|| Let me demonstrate these functions using '''OMShell'''.  
+
|| Now let me demonstrate these functions using '''OMShell'''.  
  
 
Let me go back to '''OMShell'''.
 
Let me go back to '''OMShell'''.
Line 227: Line 229:
  
 
Type // '''fill(5,2,2)''' //
 
Type // '''fill(5,2,2)''' //
|| Type '''fill(within parentheses) 5 (comma) 2 (comma) 2'''.  
+
|| Type '''fill'''(within parentheses)''' 5''' (comma) '''2''' (comma) '''2'''.  
  
This command generates a two by two matrix with all its elements '''5'''.  
+
This command generates a two by two matrix with all its elements be in '''5'''.  
  
The first arguments represents element to be filled.  
+
The first arguments represents the element to be filled within the array.  
  
The second argument represents first dimension of the array.
+
'''2''' represents the size of first dimension.
  
whereas the third argument, that is, '''2''', represents second dimension of the array.
+
and the third argument '''2''', represents the size of second dimension.
  
|-
+
Now press Enter.
||
+
 
|| Hence, using the function '''fill''', one may generate arrays of any dimension with a given number.  
+
The result is as expected.
 +
 
 +
Elements with one set of curly braces represent a row.
 +
 
 +
Hence this matrix has two rows and two columns.
  
 
|-
 
|-
 
|| Type // '''zeros(2,2)''' //
 
|| Type // '''zeros(2,2)''' //
|| Let us use '''zeros()''' function to create a (two by two) matrix with all its elements zero.
+
|| Now let us use '''zeros()''' function to create a (two by two) matrix with all its elements '''zero'''.
  
Type '''zeros (within parentheses) 2 (comma) ''' and press '''Enter'''.
+
Type '''zeros''' (within parentheses) '''2''' (comma) '''2''' and press '''Enter'''.
  
 
The result is as expected.
 
The result is as expected.
Line 253: Line 259:
 
|| Let us now try '''identity''' function.  
 
|| Let us now try '''identity''' function.  
  
Type '''identity(3)''' and press '''Enter'''.
+
Type '''identity(3)'''.
  
Note that it creates a 3 (by) 3 identity matrix.
+
This creates an identity matrix which is '''3''' (by) '''3''' in its size.
  
 
|-
 
|-
Line 265: Line 271:
 
|| Let us create two matrices and perform '''arithmetic operations''' on them.
 
|| Let us create two matrices and perform '''arithmetic operations''' on them.
  
Type '''a (colon) (equals) (within square brackets) 1 (comma) 2 (semicolon) 3 (comma) 4''' and press '''Enter.  
+
Type '''a''' (colon) (equals) (within square brackets) '''1''' (comma) '''2''' (semicolon) '''3''' (comma) '''4'''.  
  
'''Comma''' is used to separate elements in a row whereas '''semi-colon''' is uses to separate rows themselves.
+
'''Comma''' is used to separate elements in a row whereas '''semi-colon''' is used to separate rows themselves.
 +
 
 +
Now press '''Enter'''.
  
 
|-
 
|-
 
|| Type // '''b:=identity(2) '''//
 
|| Type // '''b:=identity(2) '''//
|| Type '''b (colon) (equals) identity (within brackets) 2''' and press '''Enter'''.
+
|| Type '''b''' (colon) (equals) '''identity (2)'''.
 +
 
 +
This generates a '''2 by 2''' identity matrix.
  
 
|-
 
|-
 
|| Type // '''a + b '''//
 
|| Type // '''a + b '''//
|| Type '''a (plus) b''' and press '''Enter'''.
+
|| Now let us performs arithmetic operations on '''a''' and '''b'''.
 +
 
 +
Type '''a''' (plus) '''b''' and press '''Enter'''.
  
 
This performs matrix addition.
 
This performs matrix addition.
Line 281: Line 293:
 
|-
 
|-
 
|| Type // '''a * b''' //
 
|| Type // '''a * b''' //
|| Type '''a (asterisk) b''' and press '''Enter'''.
+
|| Type '''a''' (asterisk)''' b'''.
  
 
This performs matrix multiplication.
 
This performs matrix multiplication.
 +
 +
Press '''Enter'''.
  
 
|-
 
|-
 
|| Type // '''a .* b''' //
 
|| Type // '''a .* b''' //
|| Type '''a (dot) (asterisk) b'''.
+
|| Type '''a''' (dot) (asterisk) '''b''' and press '''Enter'''.
 
+
Press '''Enter'''.
+
  
 
This performs element-wise multiplication of the two matrices.
 
This performs element-wise multiplication of the two matrices.
Line 299: Line 311:
 
|-
 
|-
 
||  
 
||  
|| Let me switch back to the slides.
+
|| Now let me switch back to the slides.
  
 
|-
 
|-
Line 305: Line 317:
  
 
'''Reduction Functions'''
 
'''Reduction Functions'''
|| '''Reduction functions''' take an '''array''' as '''input''' and return '''scalar''' as '''output'''.
+
|| '''Reduction functions''' take '''array''' as '''input''' and return '''scalar''' as '''output'''.
  
'''min()''' function returns the minimum value in an array.
+
'''min()''' is a function which returns the smallest value in an array.
  
'''max()''' function returns the maximum value in an array.
+
Similarly, '''max()''' function returns the largest value in an array.
  
'''sum()''' function returns the sum of all elements in an array.
+
'''sum()''' returns the sum of all elements
  
'''product()''' function returns the product of all elements in an array.
+
and '''product()''' returns the product of all elements.
  
 
|-
 
|-
Line 321: Line 333:
 
|-
 
|-
 
|| '''x = [3,4;5,6]'''
 
|| '''x = [3,4;5,6]'''
|| Let me create a new matrix '''x'''.  
+
|| Let me create a new matrix.
  
Type '''x (colon)(equals) 3 (comma) 4 (semicolon) 5 (comma) 6'''.
+
'''x (colon)(equals) (within square brackets) 3 (comma) 4 (semicolon) 5 (comma) 6'''.
  
 
|-
 
|-
 
|| Type // '''min(x)''' //
 
|| Type // '''min(x)''' //
|| Let us apply each one of those functions on '''x'''.
+
|| Type '''min (x)''' to obtain the minimum value of '''x'''.
 
+
Type '''min (within brackets) x''' and press '''Enter'''.
+
 
+
It returns the minimum value in '''x'''.
+
  
 
|-
 
|-
 
|| Type // '''max(x)''' //
 
|| Type // '''max(x)''' //
|| Type '''max (within brackets) x''' and press '''Enter'''.
+
|| Type '''max (x)''' to obtain the largest value in array '''x'''..
 
+
It returns the maximum value in '''x'''.
+
  
 
|-
 
|-
 
|| Type // '''sum(x) '''//
 
|| Type // '''sum(x) '''//
|| Type '''sum (within brackets) x'''.
+
|| Similarly type '''sum (x)''' to obtain the sum of all elements.
 
+
Press '''Enter'''.
+
 
+
It returns the sum of all elements in '''x'''.
+
  
 
|-
 
|-
 
|| Type // '''product(x)''' //
 
|| Type // '''product(x)''' //
|| Type '''product (within brackets) x'''.
+
|| and '''product (x)''' to obtain the product of individual elements min array '''x'''.
 
+
Press '''Enter'''
+
 
+
It returns the product of all elements in '''x'''.
+
  
 
|-
 
|-
 
||  
 
||  
|| Let me go back to the slides.
+
|| Let me go back to the slides once again.
  
 
|-
 
|-
Line 365: Line 363:
 
|| Let us now discuss various other functions that take an '''array''' as '''input'''.
 
|| Let us now discuss various other functions that take an '''array''' as '''input'''.
  
'''abs()''' is a function that returns an array with the absolute values of all its elements.
+
'''abs()''' is a function which returns an array with the absolute values of all its elements.
  
 
'''size()''' returns a vector with the size of each dimension.
 
'''size()''' returns a vector with the size of each dimension.
  
'''ndims()''' function returns the number of dimensions in an array.
+
'''ndims()''' returns the number of dimensions in an array.
  
 
|-
 
|-
Line 385: Line 383:
  
 
'''Assignment'''
 
'''Assignment'''
|| As an assignment, apply '''abs(), size()''' and '''ndims()''' to an array..
+
|| As an assignment, apply '''abs(), '''ndims()''' and '''size()''' functions to an array.
  
 
Secondly, we have used a two-dimensional array or matrix as an argument to most of the functions.  
 
Secondly, we have used a two-dimensional array or matrix as an argument to most of the functions.  
  
As an assignment, implement all these functions with higher dimensional arrays.
+
As an assignment, implement all these functions with three dimensional arrays.
 
+
|-
+
||
+
|| This brings us to the end of this tutorial.
+
  
 
|-
 
|-
Line 399: Line 393:
  
 
About the '''Spoken Tutorial project'''
 
About the '''Spoken Tutorial project'''
|| Watch the video available at the link shown below.
+
|| Watch the video available at the following link:
  
 
[http://spoken-tutorial.org/ http][http://spoken-tutorial.org/ ://][http://spoken-tutorial.org/ spoken][http://spoken-tutorial.org/ -][http://spoken-tutorial.org/ tutorial][http://spoken-tutorial.org/ .][http://spoken-tutorial.org/ org] /What\_is\_a\_Spoken\_Tutorial
 
[http://spoken-tutorial.org/ http][http://spoken-tutorial.org/ ://][http://spoken-tutorial.org/ spoken][http://spoken-tutorial.org/ -][http://spoken-tutorial.org/ tutorial][http://spoken-tutorial.org/ .][http://spoken-tutorial.org/ org] /What\_is\_a\_Spoken\_Tutorial
 +
 +
Its summarises the '''Spoken Tutorial''' project.
  
 
|-
 
|-
Line 417: Line 413:
  
 
'''Forum to answer questions'''
 
'''Forum to answer questions'''
|| If you have questions in this tutorial, please visit the webpage mentioned.
+
|| If you have questions in this spoken tutorial, please visit the webpage mentioned.
  
 
|-
 
|-
Line 423: Line 419:
  
 
'''Textbook Companion Project'''
 
'''Textbook Companion Project'''
|| We coordinate coding of solved examples from popular books.  
+
|| We coordinate coding of solved examples of popular books.  
  
We give honorarium to contributors.
+
Please contact us.
 
+
Please visit our website.
+
  
 
|-
 
|-
Line 433: Line 427:
  
 
'''Lab Migration Project'''
 
'''Lab Migration Project'''
|| We help migrate labs from commercial simulators to '''OpenModelica'''.
+
|| We help migrate commercial simulator labs to '''OpenModelica'''.
  
 
|-
 
|-
Line 439: Line 433:
  
 
'''Acknowledgements'''
 
'''Acknowledgements'''
|| '''Spoken Tutorial Project''' is supported by '''NMEICT, MHRD''' Government of India.
+
|| '''Spoken Tutorial Project''' is funded by '''NMEICT, MHRD''' Government of India.
  
 
|-
 
|-
Line 445: Line 439:
  
 
'''Thanks'''
 
'''Thanks'''
|| We thank '''OpenModelica''' development team for their support.
+
|| We thank the development team of '''OpenModelica''' for their support.
  
 
|-
 
|-
 
||  
 
||  
|| Thank you for joining me in this tutorial.  
+
|| Thank you.
 
+
Goodbye!
+

Revision as of 17:45, 21 March 2016

Visual Cue Narration
Slide:

Title Slide

Welcome to the Spoken Tutorial on Array Functions and Operations.
Slide:

Learning Objectives

In this tutorial, we are going to learn:
  • how to use OMShell
  • how to use array construction functions.
  • how to perform arithmetic operations on vectors and matrices.
  • how to use array conversion functions.
Slide:

System Requirements

To record this tutorial, I am using
  • OpenModelica 1.9.2
  • Ubuntu Operating System version 14.04 and
  • gedit

Windows users may use any text editor like Notepad instead of gedit.

Slide:

Prerequisites

To understand and practice this tutorial, you need
  • knowledge of function and array declaration in Modelica.
  • Prerequisite tutorials are mentioned on our website.
  • Please go through them.
Slide:

OMShell

let us learn more about OMShell now.

OMShell is an interactive command line tool.

It is a part of OpenModelica.

OpenModelica compiler can be invoked using commands typed in OMShell.

It can be used for loading classes and simulating them.

Functions can also be called in OMShell.

We shall now use classes named polynomialEvaluatorUsingVectors and functionTester to demonstrate OMShell.

These classes were discussed in previous tutorials.

For more information on these classes, please watch the prerequisite tutorials.

All the commands to be used in this tutorial are provided in a file named OMShell-commands.txt.

You may locate and download all the code files available on our website.

Please save all these code files in one directory for easy access.

Click on Search icon in Icon Tray. Now let me launch OMShell.

To open OMShell on Ubuntu Operating System,

Click on Dash Home icon at top left in the launcher.

Type OMShell in the address box. Type OMShell in the address box.

Click on OMShell icon.

In Windows, you may find the icon in Start menu.

/* OMShell */ Now let us learn a few useful commands.

Firstly, go to the location where you saved the text file named OMShell-commands.txt and open it.

Note that this file has all the commands to be used in this tutorial.

Hence, you may refer to this file whenever in doubt.

Now let me switch to OMShell.

Type // cd() // Type cd open and close parentheses.

Press Enter to display the result produced on execution of the command.

This prints the path to current directory.
Type // cd(“path”) // Now let us change current directory to the location where you have saved the code files.

Let me change directory on my system.

Type cd(open and close parentheses) (within double quotes), specify the path.

Press Enter.

Note that a Windows path uses forward slash unlike the backward slash used in Ubuntu.

Windows users need to be cautious of this fact.

Type // loadFile(“polynomialEvaluatorUsingVectors.mo”) // Now let us load polynomialEvaluatorUsingVectors function.

Type loadFile (within parentheses) (within double quotes) polynomialEvaluatorUsingVectors.mo.

Note that F is upper-case in loadFile() command.

This command can be used to load class or model files with a file extension of .mo.

Now press Enter.

// true // If the file is found, OMShell returns true.
Type // polynomialEvaluatorUsingVectors(10) // Now let us call this function interactively.

Type polynomialEvaluatorUsingVectors (with an argument of) 10.

Press Enter.

This command takes an input argument of 10 units and displays the result.
Type // loadFile(“functionTester.mo”) // Let me now load functionTester class.

Type loadFile (open and close parentheses) (within double quotes) functionTester.mo.

Press Enter.

Type // simulate(functionTester,startTime=0,stopTime=1) // Now let us simulate functionTester class.

Type simulate (within parentheses) functionTester (comma) startTime (equals) 0 stopTime (equals) 1.

Press Enter.

The simulation is now complete.

Let us plot variable z from functionTester class.

Type // plot({z}) // Type plot (within parentheses) (within curly braces) z and press Enter.

This command generates a plot of variable z vs time.

Now let me go back to the slides.
Slide:

Array Construction Functions

  • fill()
  • zeros()
  • identity()
Array construction functions are used to construct arrays of given size.

Now let us take a look at a few array construction functions.

We will also practice them using OMShell.

fill() is function used to create an array with all the elements same.

The syntax for fill is as shown.

First argument represents the number which fills the array.

Remaining arguments represent the size of each dimension.

zeros() is a function used to create an array filled with zeros.

Syntax for zeros() function is as shown.

Arguments represent the size of each dimension of the array.

identity() function creates an identity matrix.

It takes one argument that represents the size of both dimensions.

Now let me demonstrate these functions using OMShell.

Let me go back to OMShell.

/* OMShell */

Type // fill(5,2,2) //

Type fill(within parentheses) 5 (comma) 2 (comma) 2.

This command generates a two by two matrix with all its elements be in 5.

The first arguments represents the element to be filled within the array.

2 represents the size of first dimension.

and the third argument 2, represents the size of second dimension.

Now press Enter.

The result is as expected.

Elements with one set of curly braces represent a row.

Hence this matrix has two rows and two columns.

Type // zeros(2,2) // Now let us use zeros() function to create a (two by two) matrix with all its elements zero.

Type zeros (within parentheses) 2 (comma) 2 and press Enter.

The result is as expected.

Type // identity(3) // Let us now try identity function.

Type identity(3).

This creates an identity matrix which is 3 (by) 3 in its size.

We can also perform arithmetic operations and use assignment statements in OMShell.
Type // a:=[1,2;3,4] // Let us create two matrices and perform arithmetic operations on them.

Type a (colon) (equals) (within square brackets) 1 (comma) 2 (semicolon) 3 (comma) 4.

Comma is used to separate elements in a row whereas semi-colon is used to separate rows themselves.

Now press Enter.

Type // b:=identity(2) // Type b (colon) (equals) identity (2).

This generates a 2 by 2 identity matrix.

Type // a + b // Now let us performs arithmetic operations on a and b.

Type a (plus) b and press Enter.

This performs matrix addition.

Type // a * b // Type a (asterisk) b.

This performs matrix multiplication.

Press Enter.

Type // a .* b // Type a (dot) (asterisk) b and press Enter.

This performs element-wise multiplication of the two matrices.

Note that it is not necessary to define data-types of variables used in OMShell.
Now let me switch back to the slides.
Slide:

Reduction Functions

Reduction functions take array as input and return scalar as output.

min() is a function which returns the smallest value in an array.

Similarly, max() function returns the largest value in an array.

sum() returns the sum of all elements

and product() returns the product of all elements.

Let me switch to OMShell to demonstrate these functions.
x = [3,4;5,6] Let me create a new matrix.

x (colon)(equals) (within square brackets) 3 (comma) 4 (semicolon) 5 (comma) 6.

Type // min(x) // Type min (x) to obtain the minimum value of x.
Type // max(x) // Type max (x) to obtain the largest value in array x..
Type // sum(x) // Similarly type sum (x) to obtain the sum of all elements.
Type // product(x) // and product (x) to obtain the product of individual elements min array x.
Let me go back to the slides once again.
Slide:

Miscellaneous functions

Let us now discuss various other functions that take an array as input.

abs() is a function which returns an array with the absolute values of all its elements.

size() returns a vector with the size of each dimension.

ndims() returns the number of dimensions in an array.

This brings us to the end of this tutorial.

In this tutorial, we used OMShell to interactively demonstrate array functions.

These functions are part of Modelica language specification.

Hence, they may be used while writing classes in OMEdit as well.

Slide:

Assignment

As an assignment, apply abs(), ndims() and size() functions to an array.

Secondly, we have used a two-dimensional array or matrix as an argument to most of the functions.

As an assignment, implement all these functions with three dimensional arrays.

Slide:

About the Spoken Tutorial project

Watch the video available at the following link:

http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial

Its summarises the Spoken Tutorial project.

Slide:

Spoken Tutorial Workshops

We conducts workshops using spoken tutorials.

Give certificates.

Please contact us.

Slide:

Forum to answer questions

If you have questions in this spoken tutorial, please visit the webpage mentioned.
Slide:

Textbook Companion Project

We coordinate coding of solved examples of popular books.

Please contact us.

Slide:

Lab Migration Project

We help migrate commercial simulator labs to OpenModelica.
Slide:

Acknowledgements

Spoken Tutorial Project is funded by NMEICT, MHRD Government of India.
Slide:

Thanks

We thank the development team of OpenModelica for their support.
Thank you.

Contributors and Content Editors

Kaushik Datta, Nancyvarkey