Dasar Pemrograman di Maple.docx

5
Dasar Pemrograman di Maple Kita telah mengalami beberapa blok bangunan untuk pemrograman di Maple.. Sebagai contoh, kita melihat bagaimana menggunakan untuk loop untuk mengisi entri-entri dari matriks. Kami sekarang akan menjelaskan bagaimana untuk menulis fungsi sendiri callable di Maple. Contoh pertama kami akan fungsi untuk menghitung faktorial of an integer dari integer . . fact := proc(n) local i,prod; if ( n < 0 ) then return("n must be positive"); end; prod := 1; for i from 1 to n do prod := i * prod; end; return(prod); end; Baris pertama di atas memberitahu Maple bahwa nama fungsi kita adalah faktorial dan bahwa dibutuhkan satu masukan. Untuk memanggil fungsi kita setelah kita telah memasuki teks di atas ke Maple, kita cukup ketik fact(3); fact(-1); # 6 # "n must be positive" # "N harus positif" Baris kedua dalam prosedur kita adalah ada sehingga Maple tahu hanya menggunakan variabel ditunjukkan dalam panggilan fungsi untuk fakta dan tempat lain dalam worksheet Anda. Untuk implementasi rekursif dari . Salah satu memiliki kode berikut. fact := proc(n) if ( n = 1 ) then return(1); end; return(n * fact(n-1)); end; Tipe lain dari loop adalah loop sementara.

description

Mata Kuliah semester 1

Transcript of Dasar Pemrograman di Maple.docx

Page 1: Dasar Pemrograman di Maple.docx

Dasar Pemrograman di MapleKita telah mengalami beberapa blok bangunan untuk pemrograman di Maple.. Sebagai contoh, kita melihat bagaimana menggunakan untuk loop untuk mengisi entri-entri dari matriks. Kami sekarang akan menjelaskan bagaimana untuk menulis fungsi sendiri callable di Maple. Contoh pertama kami akan fungsi untuk menghitung faktorial of an integer dari integer . .

fact := proc(n) local i,prod; if ( n < 0 ) then return("n must be positive"); end; prod := 1; for i from 1 to n do prod := i * prod; end; return(prod);end;

Baris pertama di atas memberitahu Maple bahwa nama fungsi kita adalah faktorial dan bahwa dibutuhkan satu masukan. Untuk memanggil fungsi kita setelah kita telah memasuki teks di atas ke Maple, kita cukup ketik

fact(3);fact(-1); # 6 # "n must be positive" # "N harus positif"

Baris kedua dalam prosedur kita adalah ada sehingga Maple tahu hanya menggunakan variabel ditunjukkan dalam panggilan fungsi untuk fakta dan tempat lain dalam worksheet Anda. Untuk implementasi rekursif dari . Salah satu memiliki kode berikut.

fact := proc(n) if ( n = 1 ) then return(1); end; return(n * fact(n-1));end;

Tipe lain dari loop adalah loop sementara.

collatz := proc(n) local m,count; count := 0; m := n; while ( m > 1 ) do if ( modp(m,2) = 0 ) then m := m/2; else m := 3*m+1; end; count := count + 1; end;

Page 2: Dasar Pemrograman di Maple.docx

return(count);end;

Fungsi ini mengembalikan jumlah iterasi yang diperlukan untuk membawa integer down to ke menggunakan Collatz kekambuhan. Ini adalah masalah terbuka yang mendalam untuk menentukan apakah loop sementara di atas selalu istirahat.

Akhirnya, kami menyebutkan combinat paket yang berguna di Maple untuk mengindeks lebih dari permutasi dan kombinasi.

with(combinat);

# indexing over all combinationsC := choose(5,2);for c in C do print(c);end;

# indexing over all permutationsP := permute(3);for p in P do print(p);end;

# indexing over all subsetsS := subsets({x,y,z}):while not S[finished] do S[nextvalue]() end;

Page 3: Dasar Pemrograman di Maple.docx

Dasar dalam Bahasa Inggris

fact := proc(n) local i,prod; if ( n < 0 ) then return("n must be positive"); end; prod := 1; for i from 1 to n do prod := i * prod; end; return(prod);end;

The first line above tells Maple that the name of our function is fact and that it takes one input. To call our function once we have entered the text above into Maple, we simply type

fact(3);fact(-1); # 6# "n must be positive"

The second line in our procedure is there so that Maple knows only to use the indicated variables within a function call to fact and nowhere else in your worksheet. For a recursive implementation of , one has the following code.

fact := proc(n) if ( n = 1 ) then return(1); end; return(n * fact(n-1));end;

Another type of loop is the while loop.

collatz := proc(n) local m,count; count := 0; m := n; while ( m > 1 ) do if ( modp(m,2) = 0 ) then m := m/2; else m := 3*m+1; end; count := count + 1; end;return(count);end;

This function returns the number of iterations required to bring an integer down to using the Collatz recurrence. It is a deep open problem to determine if the while loop above always breaks.

Finally, we mention a useful package combinat in Maple for indexing over permutations and combinations.

Page 4: Dasar Pemrograman di Maple.docx

with(combinat);

# indexing over all combinationsC := choose(5,2);for c in C do print(c);end;

# indexing over all permutationsP := permute(3);for p in P do print(p);end;

# indexing over all subsetsS := subsets({x,y,z}):while not S[finished] do S[nextvalue]() end;