Difference between revisions of "OpenModelica/C2/Array-Functions-and-Operations/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
|||
Line 5: | Line 5: | ||
|- | |- | ||
|| 00:01 | || 00:01 | ||
− | || Welcome to the Spoken Tutorial on '''Array Functions and Operations'''. | + | || Welcome to the '''Spoken Tutorial''' on '''Array Functions and Operations'''. |
|- | |- | ||
|| 00:07 | || 00:07 | ||
− | || In this tutorial, we are going to learn: how to use '''OMShell''' , how to use '''array construction functions''' | + | || In this tutorial, we are going to learn: |
+ | how to use '''OMShell''' , | ||
+ | how to use '''array construction functions''', | ||
|- | |- | ||
|| 00:17 | || 00:17 | ||
− | || how to perform | + | || how to perform arithmetic operations on '''vector'''s and matrices, |
|- | |- | ||
Line 21: | Line 23: | ||
|- | |- | ||
|| 00:27 | || 00:27 | ||
− | || To record this tutorial, I am using | + | || To record this tutorial, I am using '''OpenModelica 1.9.2''', '''Ubuntu Operating System''' version 14.04 and '''gedit'''. |
|- | |- | ||
|| 00:40 | || 00:40 | ||
− | ||'''Windows''' users may use any text editor like '''Notepad''' instead of '''gedit'''. | + | ||'''Windows''' users may use any text-editor like '''Notepad''' instead of '''gedit'''. |
|- | |- | ||
Line 41: | Line 43: | ||
|- | |- | ||
|| 01:06 | || 01:06 | ||
− | ||'''OMShell''' is an interactive command line tool. | + | ||'''OMShell''' is an interactive '''command line''' tool. |
|- | |- | ||
Line 53: | Line 55: | ||
|- | |- | ||
|| 01:20 | || 01:20 | ||
− | ||It can be used for | + | ||It can be used for '''load'''ing '''classes''' and simulating them. |
|- | |- | ||
Line 81: | Line 83: | ||
|- | |- | ||
|| 02:03 | || 02:03 | ||
− | ||Please save all these code files in one directory for easy access. | + | ||Please '''save''' all these code files in one directory for easy access. |
|- | |- | ||
Line 89: | Line 91: | ||
|- | |- | ||
|| 02:12 | || 02:12 | ||
− | ||To open '''OMShell''' on '''Ubuntu''' Operating System, click on '''Dash Home''' icon at top left in the launcher. | + | ||To open '''OMShell''' on '''Ubuntu''' Operating System, click on '''Dash Home''' icon at top left in the '''launcher'''. |
|- | |- | ||
|| 02:21 | || 02:21 | ||
− | || Type '''OMShell''' in the search bar. | + | || Type '''OMShell''' in the '''search bar'''. |
|- | |- | ||
Line 105: | Line 107: | ||
|- | |- | ||
|| 02:33 | || 02:33 | ||
− | || Now, let us learn a few useful | + | || Now, let us learn a few useful '''command'''s. |
|- | |- | ||
Line 113: | Line 115: | ||
|- | |- | ||
|| 02:47 | || 02:47 | ||
− | ||Note that this file has all the ''' | + | ||Note that this file has all the '''command'''s to be used in this tutorial. |
|- | |- | ||
Line 125: | Line 127: | ||
|- | |- | ||
|| 03:00 | || 03:00 | ||
− | || Type '''cd open and close parentheses | + | || Type: '''cd''' open and close parentheses. |
|- | |- | ||
Line 133: | Line 135: | ||
|- | |- | ||
|| 03:11 | || 03:11 | ||
− | || This prints the path to current directory. | + | || This prints the '''path''' to current directory. |
|- | |- | ||
||03:15 | ||03:15 | ||
− | || Now let us 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'''. |
|- | |- | ||
Line 145: | Line 147: | ||
|- | |- | ||
|| 03:25 | || 03:25 | ||
− | ||Type '''cd''' | + | ||Type: '''cd''' open and close parentheses, within double quotes specify the '''path'''. Press '''Enter'''. |
|- | |- | ||
|| 03:38 | || 03:38 | ||
− | || Note that a '''Windows''' | + | || Note that a '''Windows path''' uses '''forward slash''' unlike the '''backward slash''' used in '''Ubuntu'''. |
|- | |- | ||
Line 157: | Line 159: | ||
|- | |- | ||
|| 03:51 | || 03:51 | ||
− | || Now let us load '''polynomialEvaluatorUsingVectors''' function. | + | || Now, let us load '''polynomialEvaluatorUsingVectors''' function. |
|- | |- | ||
|| 03:57 | || 03:57 | ||
− | ||Type '''loadFile''' | + | ||Type '''loadFile''' within parentheses within double quotes '''polynomialEvaluatorUsingVectors.mo'''. |
|- | |- | ||
Line 169: | Line 171: | ||
|- | |- | ||
||04:16 | ||04:16 | ||
− | ||This command can be used to | + | ||This '''command''' can be used to '''load''' class or '''model''' files with a file extension of '''.mo'''. |
|- | |- | ||
Line 181: | Line 183: | ||
|- | |- | ||
|| 04:33 | || 04:33 | ||
− | || Now let us call this function interactively. | + | || Now, let us call this function interactively. |
|- | |- | ||
|| 04:37 | || 04:37 | ||
− | ||Type '''polynomialEvaluatorUsingVectors''' | + | ||Type: '''polynomialEvaluatorUsingVectors''' with an argument of '''10'''. Press '''Enter'''. |
|- | |- | ||
|| 04:47 | || 04:47 | ||
− | || This command takes an input argument of '''10''' units and displays the result. | + | || This command takes an '''input argument''' of '''10''' units and displays the result. |
|- | |- | ||
Line 197: | Line 199: | ||
|- | |- | ||
|| 04:59 | || 04:59 | ||
− | ||Type '''loadFile''' | + | ||Type: '''loadFile''' open and close parentheses within double quotes '''functionTester.mo'''. Press '''Enter'''. |
|- | |- | ||
|| 05:12 | || 05:12 | ||
− | || Now let us simulate '''functionTester class'''. | + | || Now, let us simulate '''functionTester class'''. |
|- | |- | ||
|| 05:16 | || 05:16 | ||
− | ||Type '''simulate''' | + | ||Type '''simulate''' within parentheses '''functionTester''' comma '''startTime''' equals '''0 stopTime''' equals '''1'''. Press '''Enter'''. |
|- | |- | ||
Line 213: | Line 215: | ||
|- | |- | ||
||05:35 | ||05:35 | ||
− | ||Let us plot variable '''z''' from '''functionTester''' class. | + | ||Let us '''plot''' variable '''z''' from '''functionTester''' class. |
|- | |- | ||
|| 05:40 | || 05:40 | ||
− | || Type '''plot''' | + | || Type '''plot''' within parentheses within curly braces '''z''' and press '''Enter'''. |
|- | |- | ||
|| 05:50 | || 05:50 | ||
− | ||This command generates a plot of variable '''z''' vs '''time'''. | + | ||This '''command''' generates a '''plot''' of variable '''z''' vs '''time'''. |
|- | |- | ||
|| 05:56 | || 05:56 | ||
− | || Now let me go back to the | + | || Now let me go back to the '''slide'''s. |
|- | |- | ||
Line 241: | Line 243: | ||
|- | |- | ||
|| 06:15 | || 06:15 | ||
− | ||'''fill()''' is function used to create an '''array''' with all the elements same. | + | ||'''fill()''' is a function used to create an '''array''' with all the elements same. The syntax for '''fill''' is as shown. |
|- | |- | ||
Line 253: | Line 255: | ||
|- | |- | ||
|| 06:34 | || 06:34 | ||
− | ||'''zeros()''' is a function 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. |
|- | |- | ||
Line 265: | Line 267: | ||
|- | |- | ||
|| 07:02 | || 07:02 | ||
− | || Now let me demonstrate these functions using '''OMShell'''. | + | || Now, let me demonstrate these functions using '''OMShell'''. |
|- | |- | ||
Line 273: | Line 275: | ||
|- | |- | ||
||07:09 | ||07:09 | ||
− | || Type '''fill''' | + | || Type: '''fill''' within parentheses ''' 5''' comma '''2''' comma '''2'''. |
|- | |- | ||
Line 281: | Line 283: | ||
|- | |- | ||
|| 07:24 | || 07:24 | ||
− | ||The first ''' | + | ||The first '''argument''' represents the '''element''' to be filled within the '''array'''. |
|- | |- | ||
|| 07:30 | || 07:30 | ||
− | ||'''2''' represents the size of first dimension | + | ||'''2''' represents the size of first dimension |
|- | |- | ||
Line 309: | Line 311: | ||
|- | |- | ||
|| 07:57 | || 07:57 | ||
− | || Now let us use '''zeros() function''' to create a | + | || Now, let us use '''zeros() function''' to create a two by two matrix with all its elements '''zero'''. |
|- | |- | ||
|| 08:05 | || 08:05 | ||
− | || Type '''zeros''' | + | || Type: '''zeros''' within parentheses '''2''' comma '''2''' and press '''Enter'''. |
|- | |- | ||
Line 325: | Line 327: | ||
|- | |- | ||
|| 08:19 | || 08:19 | ||
− | ||Type '''identity(3)'''. | + | ||Type: '''identity(3)'''. |
|- | |- | ||
|| 08:23 | || 08:23 | ||
− | ||This creates an '''identity''' matrix which is '''3''' | + | ||This creates an '''identity''' matrix which is '''3''' by '''3''' in its size. |
|- | |- | ||
|| 08:29 | || 08:29 | ||
− | || We can also perform | + | || We can also perform arithmetic operations and use '''assignment statements''' in '''OMShell'''. |
|- | |- | ||
|| 08:36 | || 08:36 | ||
− | ||Let us create two matrices and perform | + | ||Let us create two matrices and perform arithmetic operations on them. |
|- | |- | ||
|| 08:42 | || 08:42 | ||
− | || Type '''a''' | + | || Type: '''a''' colon equals within square brackets '''1''' comma '''2''' semicolon '''3''' comma '''4'''. |
|- | |- | ||
|| 08:54 | || 08:54 | ||
− | ||''' | + | ||Comma is used to separate '''element'''s in a row |
|- | |- | ||
|| 08:58 | || 08:58 | ||
− | ||whereas | + | ||whereas semi-colon is used to separate rows themselves. Now press '''Enter'''. |
|- | |- | ||
|| 09:07 | || 09:07 | ||
− | || Type '''b''' | + | || Type: '''b''' colon equals '''identity of 2'''. |
|- | |- | ||
Line 361: | Line 363: | ||
|- | |- | ||
|| 09:19 | || 09:19 | ||
− | || Now let us perform | + | || Now, let us perform arithmetic operations on '''a''' and '''b'''. |
|- | |- | ||
|| 09:24 | || 09:24 | ||
− | ||Type '''a''' | + | ||Type: '''a''' plus '''b''' and press '''Enter'''. |
|- | |- | ||
Line 373: | Line 375: | ||
|- | |- | ||
|| 09:32 | || 09:32 | ||
− | || Type '''a''' | + | || Type '''a''' asterisk ''' b'''. |
|- | |- | ||
Line 381: | Line 383: | ||
|- | |- | ||
|| 09:42 | || 09:42 | ||
− | || Type '''a''' | + | || Type: '''a''' dot asterisk '''b''' and press '''Enter'''. |
|- | |- | ||
Line 417: | Line 419: | ||
|- | |- | ||
|| 10:41 | || 10:41 | ||
− | ||'''x | + | ||'''x colon equals within square brackets 3 comma 4 semicolon 5 comma 6'''. |
|- | |- | ||
|| 10:52 | || 10:52 | ||
− | || Type '''min (x)''' to obtain the minimum value of '''x'''. | + | || Type: '''min (x)''' to obtain the minimum value of '''x'''. |
|- | |- | ||
|| 11:00 | || 11:00 | ||
− | || Type '''max (x)''' to obtain the largest value in array '''x''' | + | || Type: '''max (x)''' to obtain the largest value in array '''x'''. |
|- | |- | ||
|| 11:08 | || 11:08 | ||
− | || Similarly type '''sum (x)''' to obtain the sum of all elements | + | || Similarly, type: '''sum (x)''' to obtain the sum of all elements |
− | + | ||
|- | |- | ||
|| 11:15 | || 11:15 | ||
Line 473: | Line 474: | ||
|- | |- | ||
|| 12:11 | || 12:11 | ||
− | || As an assignment, apply '''abs(), '''ndims()''' and '''size() | + | || As an assignment, apply '''abs(), '''ndims()''' and '''size()''' functions to an '''array'''. |
|- | |- | ||
|| 12:19 | || 12:19 | ||
− | ||Secondly, we have used a | + | ||Secondly, we have used a two-dimensional array or matrix as an '''argument''' to most of the '''functions'''. |
|- | |- | ||
|| 12:28 | || 12:28 | ||
− | ||As an assignment, implement all these '''functions''' with | + | ||As an assignment, implement all these '''functions''' with three-dimensional arrays. |
|- | |- | ||
|| 12:35 | || 12:35 | ||
− | || Watch the video available at the following link: | + | || Watch the video available at the following link: |
+ | http://spoken-tutorial.org/What_is_a_Spoken_Tutorial | ||
|- | |- | ||
|| 12:39 | || 12:39 | ||
− | || | + | ||It summarizes the '''Spoken Tutorial''' project. |
|- | |- | ||
|| 12:42 | || 12:42 | ||
− | || We conducts workshops using spoken tutorials | + | || We conducts workshops using spoken tutorials, give certificates. Please contact us. |
|- | |- |
Latest revision as of 15:46, 20 August 2018
Time | Narration |
00:01 | Welcome to the Spoken Tutorial on Array Functions and Operations. |
00:07 | In this tutorial, we are going to learn:
how to use OMShell , how to use array construction functions, |
00:17 | how to perform arithmetic operations on vectors and matrices, |
00:23 | how to use array conversion functions. |
00:27 | To record this tutorial, I am using OpenModelica 1.9.2, Ubuntu Operating System version 14.04 and gedit. |
00:40 | Windows users may use any text-editor like Notepad instead of gedit. |
00:47 | To understand and practice this tutorial, you need knowledge of function and array declaration in Modelica. |
00:56 | Prerequisite tutorials are mentioned on our website. Please go through them. |
01:02 | Let us learn more about OMShell now. |
01:06 | OMShell is an interactive command line tool. |
01:10 | It is a part of OpenModelica. |
01:13 | OpenModelica compiler can be invoked using commands typed in OMShell. |
01:20 | It can be used for loading classes and simulating them. |
01:25 | Functions can also be called in OMShell. |
01:29 | We shall now use classes named polynomialEvaluatorUsingVectors and functionTester to demonstrate OMShell. |
01:38 | These classes were discussed in previous tutorials. |
01:42 | For more information on these classes, please watch the prerequisite tutorials. |
01:48 | All the commands to be used in this tutorial are provided in a file named OMShell-commands.txt. |
01:57 | You may locate and download all the code files available on our website. |
02:03 | Please save all these code files in one directory for easy access. |
02:09 | Now let me launch OMShell. |
02:12 | To open OMShell on Ubuntu Operating System, click on Dash Home icon at top left in the launcher. |
02:21 | Type OMShell in the search bar. |
02:25 | Click on OMShell icon. |
02:28 | In Windows, you may find the icon in Start menu. |
02:33 | Now, let us learn a few useful commands. |
02:37 | Firstly, go to the location where you saved the text file named OMShell-commands.txt and open it. |
02:47 | Note that this file has all the commands to be used in this tutorial. |
02:52 | Hence, you may refer to this file whenever in doubt. |
02:57 | Now let me switch to OMShell. |
03:00 | Type: cd open and close parentheses. |
03:05 | Press Enter to display the result produced on execution of the command. |
03:11 | This prints the path to current directory. |
03:15 | Now, let us change current directory to the location where you have saved the code files. |
03:22 | Let me change directory on my system. |
03:25 | Type: cd open and close parentheses, within double quotes specify the path. Press Enter. |
03:38 | Note that a Windows path uses forward slash unlike the backward slash used in Ubuntu. |
03:46 | Windows users need to be cautious of this fact. |
03:51 | Now, let us load polynomialEvaluatorUsingVectors function. |
03:57 | Type loadFile within parentheses within double quotes polynomialEvaluatorUsingVectors.mo. |
04:11 | Note that F is upper-case in loadFile() command. |
04:16 | This command can be used to load class or model files with a file extension of .mo. |
04:25 | Now press Enter. |
04:28 | If the file is found, OMShell returns true. |
04:33 | Now, let us call this function interactively. |
04:37 | Type: polynomialEvaluatorUsingVectors with an argument of 10. Press Enter. |
04:47 | This command takes an input argument of 10 units and displays the result. |
04:55 | Let me now load functionTester class. |
04:59 | Type: loadFile open and close parentheses within double quotes functionTester.mo. Press Enter. |
05:12 | Now, let us simulate functionTester class. |
05:16 | Type simulate within parentheses functionTester comma startTime equals 0 stopTime equals 1. Press Enter. |
05:32 | The simulation is now complete. |
05:35 | Let us plot variable z from functionTester class. |
05:40 | Type plot within parentheses within curly braces z and press Enter. |
05:50 | This command generates a plot of variable z vs time. |
05:56 | Now let me go back to the slides. |
06:01 | Array construction functions are used to construct arrays of given size. |
06:06 | Now let us take a look at a few array construction functions. |
06:11 | We will also practice them using OMShell. |
06:15 | fill() is a function used to create an array with all the elements same. The syntax for fill is as shown. |
06:25 | First argument represents the number which fills the array. |
06:29 | Remaining arguments represent the size of each dimension. |
06:34 | zeros() is a function used to create an array filled with zeros. Syntax for zeros() function is as shown. |
06:44 | Arguments represent the size of each dimension of the array. |
06:50 | identity() function creates an identity matrix. It takes one argument that represents the size of both dimensions. |
07:02 | Now, let me demonstrate these functions using OMShell. |
07:06 | Let me go back to OMShell. |
07:09 | Type: fill within parentheses 5 comma 2 comma 2. |
07:16 | This command generates a two by two matrix with all its elements being 5. |
07:24 | The first argument represents the element to be filled within the array. |
07:30 | 2 represents the size of first dimension |
07:34 | and the third argument 2, represents the size of second dimension. |
07:40 | Now press Enter. |
07:43 | The result is as expected. |
07:46 | Elements with one set of curly braces represent a row. |
07:52 | Hence this matrix has two rows and two columns. |
07:57 | Now, let us use zeros() function to create a two by two matrix with all its elements zero. |
08:05 | Type: zeros within parentheses 2 comma 2 and press Enter. |
08:13 | The result is as expected. |
08:16 | Let us now try identity function. |
08:19 | Type: identity(3). |
08:23 | This creates an identity matrix which is 3 by 3 in its size. |
08:29 | We can also perform arithmetic operations and use assignment statements in OMShell. |
08:36 | Let us create two matrices and perform arithmetic operations on them. |
08:42 | Type: a colon equals within square brackets 1 comma 2 semicolon 3 comma 4. |
08:54 | Comma is used to separate elements in a row |
08:58 | whereas semi-colon is used to separate rows themselves. Now press Enter. |
09:07 | Type: b colon equals identity of 2. |
09:15 | This generates a 2 by 2 identity matrix. |
09:19 | Now, let us perform arithmetic operations on a and b. |
09:24 | Type: a plus b and press Enter. |
09:29 | This performs matrix addition. |
09:32 | Type a asterisk b. |
09:36 | This performs matrix multiplication. Press Enter. |
09:42 | Type: a dot asterisk b and press Enter. |
09:49 | This performs element-wise multiplication of the two matrices. |
09:55 | Note that it is not necessary to define data-types of variables used in OMShell. |
10:02 | Now let me switch back to the slides. |
10:06 | Reduction functions take array as input and return scalar as output. |
10:13 | min() is a function which returns the smallest value in an array. |
10:19 | 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. |
10:33 | Let me switch to OMShell to demonstrate these functions. |
10:38 | Let me create a new matrix. |
10:41 | x colon equals within square brackets 3 comma 4 semicolon 5 comma 6. |
10:52 | Type: min (x) to obtain the minimum value of x. |
11:00 | Type: max (x) to obtain the largest value in array x. |
11:08 | Similarly, type: sum (x) to obtain the sum of all elements |
11:15 | and product (x) to obtain the product of individual elements min array x. |
11:23 | Let me go back to the slides once again. |
11:27 | Let us now discuss various other functions that take an array as input. |
11:33 | abs() is a function which returns an array with the absolute values of all its elements. |
11:40 | size() returns a vector with the size of each dimension. |
11:45 | ndims() returns the number of dimensions in an array. |
11:51 | This brings us to the end of this tutorial. |
11:54 | In this tutorial, we used OMShell to interactively demonstrate array functions. |
12:01 | These functions are part of Modelica language specification. |
12:05 | Hence, they may be used while writing classes in OMEdit as well. |
12:11 | As an assignment, apply abs(), ndims() and size() functions to an array. |
12:19 | Secondly, we have used a two-dimensional array or matrix as an argument to most of the functions. |
12:28 | As an assignment, implement all these functions with three-dimensional arrays. |
12:35 | Watch the video available at the following link: |
12:39 | It summarizes the Spoken Tutorial project. |
12:42 | We conducts workshops using spoken tutorials, give certificates. Please contact us. |
12:48 | If you have questions in this spoken tutorial, please visit the webpage mentioned. |
12:54 | We coordinate coding of solved examples of popular books. Please contact us. |
13:00 | We help migrate commercial simulator labs to OpenModelica. |
13:06 | Spoken Tutorial Project is funded by NMEICT, MHRD Government of India. |
13:14 | We thank the development team of OpenModelica for their support. Thank you. |