All pastes #596861 Raw Edit

Matrices

public text v1 · immutable
#596861 ·published 2007-06-30 07:26 UTC
rendered paste body
alias mat { dialog -mt matrices matrices }
dialog matrices {
  title "Matrices"
  size -1 -1 325 65
  option dbu
  edit "", 111, 2 2 19 10, limit 2 center
  edit "", 112, 21 2 19 10, limit 2 center
  edit "", 113, 40 2 19 10, limit 2 center
  edit "", 114, 59 2 19 10, limit 2 center
  edit "", 115, 78 2 19 10, limit 2 center
  edit "", 121, 2 12 19 10, limit 2 center
  edit "", 122, 21 12 19 10, limit 2 center
  edit "", 123, 40 12 19 10, limit 2 center
  edit "", 124, 59 12 19 10, limit 2 center
  edit "", 125, 78 12 19 10, limit 2 center
  edit "", 131, 2 22 19 10, limit 2 center
  edit "", 132, 21 22 19 10, limit 2 center
  edit "", 133, 40 22 19 10, limit 2 center
  edit "", 134, 59 22 19 10, limit 2 center
  edit "", 135, 78 22 19 10, limit 2 center
  edit "", 141, 2 32 19 10, limit 2 center
  edit "", 142, 21 32 19 10, limit 2 center
  edit "", 143, 40 32 19 10, limit 2 center
  edit "", 144, 59 32 19 10, limit 2 center
  edit "", 145, 78 32 19 10, limit 2 center
  edit "", 151, 2 42 19 10, limit 2 center
  edit "", 152, 21 42 19 10, limit 2 center
  edit "", 153, 40 42 19 10, limit 2 center
  edit "", 154, 59 42 19 10, limit 2 center
  edit "", 155, 78 42 19 10, limit 2 center
  radio "+", 6, 102 16 15 10
  radio "-", 7, 102 25 15 10
  radio "*", 8, 102 34 15 10
  edit "", 211, 119 2 19 10, limit 2 center
  edit "", 212, 138 2 19 10, limit 2 center
  edit "", 213, 157 2 19 10, limit 2 center
  edit "", 214, 176 2 19 10, limit 2 center
  edit "", 215, 195 2 19 10, limit 2 center
  edit "", 221, 119 12 19 10, limit 2 center
  edit "", 222, 138 12 19 10, limit 2 center
  edit "", 223, 157 12 19 10, limit 2 center
  edit "", 224, 176 12 19 10, limit 2 center
  edit "", 225, 195 12 19 10, limit 2 center
  edit "", 231, 119 22 19 10, limit 2 center
  edit "", 232, 138 22 19 10, limit 2 center
  edit "", 233, 157 22 19 10, limit 2 center
  edit "", 234, 176 22 19 10, limit 2 center
  edit "", 235, 195 22 19 10, limit 2 center
  edit "", 241, 119 32 19 10, limit 2 center
  edit "", 242, 138 32 19 10, limit 2 center
  edit "", 243, 157 32 19 10, limit 2 center
  edit "", 244, 176 32 19 10, limit 2 center
  edit "", 245, 195 32 19 10, limit 2 center
  edit "", 251, 119 42 19 10, limit 2 center
  edit "", 252, 138 42 19 10, limit 2 center
  edit "", 253, 157 42 19 10, limit 2 center
  edit "", 254, 176 42 19 10, limit 2 center
  edit "", 255, 195 42 19 10, limit 2 center
  text "=", 54, 218 25 5 8, center
  edit "", 311, 228 2 19 10, read center
  edit "", 312, 247 2 19 10, read center
  edit "", 313, 266 2 19 10, read center
  edit "", 314, 285 2 19 10, read center
  edit "", 315, 304 2 19 10, read center
  edit "", 321, 228 12 19 10, read center
  edit "", 322, 247 12 19 10, read center
  edit "", 323, 266 12 19 10, read center
  edit "", 324, 285 12 19 10, read center
  edit "", 325, 304 12 19 10, read center
  edit "", 331, 228 22 19 10, read center
  edit "", 332, 247 22 19 10, read center
  edit "", 333, 266 22 19 10, read center
  edit "", 334, 285 22 19 10, read center
  edit "", 335, 304 22 19 10, read center
  edit "", 341, 228 32 19 10, read center
  edit "", 342, 247 32 19 10, read center
  edit "", 343, 266 32 19 10, read center
  edit "", 344, 285 32 19 10, read center
  edit "", 345, 304 32 19 10, read center
  edit "", 351, 228 42 19 10, read center
  edit "", 352, 247 42 19 10, read center
  edit "", 353, 266 42 19 10, read center
  edit "", 354, 285 42 19 10, read center
  edit "", 355, 304 42 19 10, read center
  text "Height:", 80, 14 54 19 8, center
  edit "", 1, 33 53 10 10, limit 1 center
  edit "", 3, 75 53 10 10, limit 1 center
  text "Width:", 83, 58 54 17 8, center
  text "Height:", 84, 129 54 19 8, center
  edit "", 2, 148 53 10 10, limit 1 center
  text "Width:", 86, 173 54 17 8, center
  edit "", 4, 190 53 10 10, limit 1 center
  button "Solve", 5, 257 52 36 12
}
on *:DIALOG:matrices:init:0:{
  did -c $dname 6
  did -o $dname 1-4 1 3
  did -h $dname 114,115,124,125,134,135,141-145,151-155,214,215,224,225,234,235,241-245,251-255
}
on *:DIALOG:matrices:edit:1-4:{
  if (!$regex($did($did),/^[1-5]?$/)) did -o $dname $did 1 3
  if ($did($did)) mat.ref
}
alias mat.ref {
  var %m = 1, %r, %c, %h, %v
  while (%m <= 2) {
    %r = 1
    while (%r <= 5) {
      %c = 1
      while (%c <= 5) {
        if (%r > $did(matrices,%m)) || (%c > $did(matrices,$calc(%m + 2))) %h = $addtok(%h,$+(%m,%r,%c),44)
        else %v = $addtok(%v,$+(%m,%r,%c),44)
        inc %c
      }
      inc %r
    }
    inc %m
  }
  if (%h) did -hr matrices %h
  if (%v) did -v matrices %v
}
on *:DIALOG:matrices:sclick:5:{
  var %m = 1, %r, %c, %0, %o
  while (%m <= 2) {
    %r = 1
    while (%r <= 5) {
      %c = 1
      while (%c <= 5) {
        %o = $+(%m,%r,%c)
        if ($did($dname,%o).visible) {
          if (!$did(%o)) {
            if (!%0) {
              var %0 = $input(Replace all empty spaces with 0's?,yqu,Question)
              if (!%0) halt
            }
            did -o $dname %o 1 0
          }
          elseif (!$regex($did(%o),/^-?\d+$/)) {
            noop $input($+(Invalid value found in:,$crlf,Matrix %m,$crlf,Row %r,$crlf,Column %c),owu,Error)
            halt
          }
        }
        inc %c
      }
      inc %r
    }
    if (!$did(%m)) {
      var %a = 5
      while (!$did($+(%m,%a,1)).visible) dec %a
      did -o $dname %m 1 %a
    }
    if (!$did($calc(%m + 2))) {
      var %a = 5
      while (!$did($+(%m,1,%a)).visible) dec %a
      did -o $dname $calc(%m + 2) 1 %a
    }
    inc %m
  }
  mat.solve
}
alias mat.solve {
  did -r $dname 311-315,321-325,331-335,341-345,351-355
  var %a = 6, %m, %o, %r, %c, %s, %x, %n
  while (!$did(%a).state) inc %a
  %o = $mid(aaaaa+-*,%a,1)
  if (%o isin -+) {
    if ($+($did(1),$did(3)) != $+($did(2),$did(4))) {
      noop $input($+(The matrices are not of the,$crlf,same order,$chr(44) and therefore cannot,$crlf,be $iif(%o == -,subtracted from,added to) one another.),owu,Error)
      halt
    }
    %r = 1
    while (%r <= $did(1)) {
      %c = 1
      while (%c <= $did(3)) {
        %s = $+(%r,%c)
        did -ov $dname $+(3,%s) 1 $calc($did($+(1,%s)) %o $did($+(2,%s)))
        inc %c
      }
      inc %r
    }
  }
  elseif (%o == *) {
    if ($did(2) != $did(3)) {
      noop $input($+(The matrices are not able to,$crlf,be multiplied together.),owu,Error)
      halt
    }
    %r = 1
    while (%r <= $did(1)) {
      %c = 1
      while (%c <= $did(4)) {
        %s = $+(%r,%c)
        %x = 1
        %n = 0
        while (%x <= $did(2)) {
          inc %n $calc($did($+(1,%r,%x)) * $did($+(2,%x,%c)))
          inc %x
        }
        did -ov $dname $+(3,%s) 1 %n
        inc %c
      }
      inc %r
    }
  }
  var %r = 1, %c
  while (%r <= 5) {
    %c = 1
    while (%c <= 5) {
      %s = $+(%r,%c)
      if ($did($+(3,%s)) !isnum) did -h $dname $+(3,%s)
      inc %c
    }
    inc %r
  }
}