Advertisement
Guest User

Untitled

a guest
May 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. method matmul(a: array2<int>, b: array2<int>) returns (c : array2<int>)
  2. requires a != null
  3. requires a.Length0 > 0 && a.Length1 > 0
  4. requires a.Length0 == a.Length1 // que pour matrices carrées
  5. requires b != null
  6. requires b.Length0 > 0 && b.Length1 > 0
  7. requires b.Length0 == b.Length1 // que pour matrices carrées
  8. requires a.Length0 == b.Length0
  9. requires a.Length1 == b.Length1
  10. ensures fresh(c)
  11. ensures c.Length0 > 0 && c.Length1 > 0
  12. ensures c.Length0 == a.Length0 == b.Length0
  13. ensures c.Length1 == c.Length1 == b.Length1
  14. ensures a == old(a)
  15. ensures b == old(b)
  16. {
  17. c := new int[a.Length0,a.Length1];
  18.  
  19. var i := 0;
  20. var n := c.Length0;
  21. while (i < n)
  22. invariant 0 <= i <= n
  23. {
  24. var j := 0;
  25. while (j < n)
  26. invariant 0 <= i <= n
  27. invariant 0 <= j <= n
  28. {
  29. var k := 0;
  30. while (k < n)
  31. invariant 0 <= i <= n
  32. invariant 0 <= j <= n
  33. invariant 0 <= k <= n
  34. {
  35. c[i,j] := c[i,j] + a[i,k] * b[k,j];
  36. k := k + 1;
  37. }
  38. j := j + 1;
  39. }
  40. i := i + 1;
  41. }
  42. }
  43.  
  44. method Main() {
  45. var a,b := new int[3,3], new int[3,3];
  46. a[0,0] := 1;
  47. a[0,1] := 2;
  48. a[0,2] := 3;
  49. a[1,0] := 4;
  50. a[1,1] := 5;
  51. a[1,2] := 6;
  52. a[2,0] := 7;
  53. a[2,1] := 8;
  54. a[2,2] := 9;
  55.  
  56. b[0,0] := 9;
  57. b[0,1] := 8;
  58. b[0,2] := 7;
  59. b[1,0] := 6;
  60. b[1,1] := 5;
  61. b[1,2] := 4;
  62. b[2,0] := 3;
  63. b[2,1] := 2;
  64. b[2,2] := 1;
  65.  
  66. var c := matmul(a, b);
  67. var x := 0;
  68.  
  69. while (x < c.Length0)
  70. invariant 0 <= x <= c.Length0
  71. {
  72. var y := 0;
  73. while (y < c.Length1)
  74. invariant 0 <= y <= c.Length1
  75. {
  76. print(c[x,y]);
  77. print(" ");
  78. y := y + 1;
  79. }
  80. print "\n";
  81. x := x + 1;
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement