วันเสาร์ที่ 30 พฤษภาคม พ.ศ. 2563

ภาษาโปรแกรมมิง ติเตอร์เรียล 9

Tutorial 9 – Scilab Programming Language


ไซแลบให้มาพร้อมภาษาการโปรแกรมแบบตัวแปล (interpreted programming language) ดังนั้นชุดของคำสั่งสามารถให้ทำงานแบบอัตโนมัติ  ภาษาโปรแกรมได้ให้ลักษณะต่างๆ มากมายของภาษาระดับสูง เช่น ลูป (for, while), การปฏิบัติงานตามเงื่อนไข(if-then-else, select) และ ฟังก์ชัน  ที่ก้าวหน้ามากที่สุดคือประโยคคำสั่งที่สามารถใช้ได้กับคำสั่งไซแลบใดๆได้

การทำงานในลูปผ่านทางตัวแปรดัชนี i จาก1 ถึง 10  และแสดงค่าในแต่ละครั้ง เราสามารถทดลองคำสั่งต่อไปนี้ที่เครื่องหมายเตรียมพร้อม:

-->for i=1:10
-->disp(i)
-->end
  1
  2
  3
  4
  5
  6
  7
  8
  9
  10

สำหรับลูป for ปิดโดยประโยคคำสั่ง end ที่สอดคล้องกัน  ทันทีที่ลูปถูกปิดลง บล็อกของประโยคคำสั่งที่อยู่ภายในลูปจะถูกสั่งปฏิบัติงาน  จากตัวอย่างข้างบน คำสั่ง disp(i) แสดงค่าของ i.

การปฏิบัติงานที่สั่งกระทำโดยใช้โครงสร้าง if-then-elseif-else  ให้ทดลองประโยคคำสั่งต่อไปนี้ที่บรรทัดคำสั่ง

 -->x=10;
-->if x<0 then disp('Negative')
-->elseif x==0 then disp('Zero')
-->else disp('Positive')   
-->end
   Positive

จากนี้ให้ผลการแสดงคำว่า Positive.  เราอาจจะสังเกตว่าประโยคคำสั่ง disp('Positive') ถูกสั่งให้ปฏิบัติ แม้ว่าก่อนที่คีย์เวิร์ด end ถูกพิมพ์เข้ามา

รายการของคำสั่งและฐานราก(primitive)ของภาษาโปรแกรมไซแลบ  สามารถแสดงให้เห็นได้โดยคำสั่ง what().  คำสั่งนั้นสร้างรายการต่อไปนี้:

if             else         for           while           end           select
case        quit         exit          return          help          what
who        pause      clear        resume         then           do
apropos  abort       break       elseif

เราสามารถเรียนรู้เกี่ยวกับแต่ละคำสั่งโดยใช้คำสั่ง help  โดยวิธีนี้  help whileจะให้สารสนเทศที่สมบูรณ์ เกี่ยวกับ while พร้อมด้วยตัวอย่าง และรายการของคำสั่งอื่นที่สัมพันธ์กัน  ที่ก้าวหน้ามากก็คือเราสามารถทดสอบทุกๆลักษณะของภาษาอย่างมีปฏิสัมพันธ์ภายใต้สิ่งแวดล้อมของไซแลบ ก่อนที่จะนำไปเก็บไว้เป็นไฟล์ต่างหากเป็นเหมือนฟังก์ชันหนึ่ง 

โอเปอร์เรเตอร์ต่อไปนี้มีให้ใช้ได้ในไซแลบ:

Symbol                           Operation                          Symbol                      Operation
 [  ]                                   Matrix definition                  ;                             Statement separator
 (  )                                   Extraction eg.  m = a(k)      ( )                            Insertion eg. a(k) = m
   '                                    Transpose                              +                             Addition
 –                                    Subtraction                            *                          Multiplication
\                                      Left division                            /                          Right division
^                                     Exponent .                               .*                        Element wise ultiplication
.\                                     Element wise left division .    ./                         Element wise right division .^                                    Element wise exponent

ต่อไปเป็นตัวกระทำบูลีนในไซแลบ
Symbol            Operation                   Symbol            Operation
==                    Equal to                      ~=                     Not equal to (also <>)
<                      Less than                     <=                     Less than or equal to
>                      Greater than                >=                      Greater than or equal to
~                      Negation
&                    Element wise AND        |                        Element wise OR

ตัวคงที่บูลีน %t  และ %T  แทน TRUE    และ %f และ %F แทน FALSE .
ต่อไปนี้เป็นตัวอย่างสำหรับตัวกระทำบูลีนปกติทั่วไป:

-->a =                                                สร้างเมทริกซ์ 3 แถว และ 4 คอลัมน์ ประกอบด้วยเลขจำนวนสุ่ม
 int(rand(3,4)*100)                            จำนวนเต็ม ระหว่าง 0 และ100.

-->a == 0                                          คืนกลับเป็นเมทริกซ์ a หนึ่งด้วยขนาดเดียวกัน ด้วยองค์ประกอบไม่ เป็น  with elements                                   T (ถ้าองค์ประกอบเท่ากับ 0) หรือ F(ถ้าองค์ประกอบไม่เทากับ0)

-->a < 20                                         สร้างเมทริกซ์ ด้วยขนาดเดียวกับเมทริกซ์ a, มีองค์ประกอบ                                                                              ไม่เป็น T (ถ้าองค์ประกอบน้อยกว่า 20) หรือ F
                                                         (ถ้าองค์ประกอบมากกว่าหรือเท่ากับ 20).

-->bool2s(a < 20)                            แรกสุดให้สร้างเมทริกซ์ของบูลีน ด้วยค่าT หรือ F ขึ้นอยู่กับ                                                                          ว่าองค์ประกอบในเมทริกซ์ a เท่ากับ 20 หรือมากกว่าหรือไม่  แล้วจึง                                                              สร้าง เมทริกซ์หนึ่งด้วยค่าจำนวน 1 หรือ 0 สอดคล้องกัน T หรือ F                                                                ตามลำดับ

-->find(a < 20)                               ให้สร้างเว็คเตอร์หนึ่งประกอบด้วยดัชนีขององค์ประกอบของ
                                                        เมทริกซ์ a ซึ่งน้อยกว่า 20


มาสร้างเมทริกซ์ขนาด 3x4   ประกอบด้วยองค์ประกอบจากการสุ่มจำนวนเต็มมีค่าระหว่าง 0 และ 100 ด้วยคำสั่ง     ด้วยคำสั่ง a = int(rand(3,4)*100). If matrix a contains the elements as shown below: [a]=[21 33 84 6 75 66 68 56 0 62 87 66], then the results of the various boolean operations are given below: To test if elements of a are zero: -->a == 0 ans =  F  F  F  F  F  F  F  F
Tutorial 9 – Scilab Programming Language | 27
  T  F  F  F To test if value of elements of a are less than 25: -->a < 25 ans =  T  F  F  T  F  F  F  F  T  F  F  F To test if elements of a are greater than or equal to 62: -->a >= 62 ans =  F  F  T  F  T  T  T  F  F  T  T  T To test if elements of a are not equal to 66:a ~= 66 ans =  T  T  T  T  T  F  T  T  T  T  T  F To test if elements of a are not equal to 66, and output 1 instead of T and 0 instead of F: -->bool2s(a ~= 66) ans =  1.  1.  1.  1.  1.  0.  1.  1.  1.  1.  1.  0. To find indices of elements of a which are greater than 70: -->b=find(a > 70) b =  2.  7.  9. The indices are counted as if a were a one dimensional vector and indices counted column wise starting from the top of the left most column. To print the values of a corresponding to these indices use the following command: -->a(b) ans =  75.  84.  87.Compound boolean operations can be performed using the AND and OR operators. To test if elements of a are greater than 20 and less than 70: -->(a > 20) & (a < 70) ans =  T  T  F  F  F  T  T  T  F  T  F  T
Tutorial 9 – Scilab Programming Language | 28
To test if elements of a are either less than 25 or greater than 75: -->(a < 25) | a > 75)  ans =  T  F  T  T  F  F  F  F  T  F  T  F It is possible to test if all elements of matrix a meet a logical test using the and() function: -->and(a > 20) ans =  F -->and(a < 100) ans =  T It is possible to test if at least one element of matrix a meets a logical test using the or() function: -->or(a > 20) ans =  T -->or(a < 200) ans =  T

ไม่มีความคิดเห็น:

แสดงความคิดเห็น