home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / self / contrib.lha / contrib / 491 / aa / poly.self < prev    next >
Encoding:
Text File  |  1993-07-22  |  9.5 KB  |  366 lines

  1. " basic polynomial types "
  2. * poly.self,v
  3. * Revision 1.13  1993/07/22  00:15:19  richards
  4. * Moved back to having event handlers per operation.
  5. * eventHandler/buttons now support substitution.
  6. * poly.self has primitive substitution handler, except that it has
  7. * no concept of context.
  8. *
  9. * Revision 1.12  1993/07/18  20:22:31  richards
  10. * Added substitution of polynomials (not quite working properly yet).
  11. * Some tests for this added to test4.
  12. * Buttons work, selections work, and eventHandling is coming along.
  13. *
  14. * Revision 1.11  1993/07/13  21:47:02  richards
  15. * July 13 checkin.
  16. *
  17. * Revision 1.10  1993/06/18  21:25:39  richards
  18. * Moved font support into viewManager.
  19. * Starting to add selection stuff into window and boite.
  20. * Boites refer properly back to the polynomial structures.
  21. *
  22. * Revision 1.9  1993/06/03  21:33:14  richards
  23. * Fixed up the redrawing of the subviews. Turned out they were being drawn off the screen
  24. * due to faulty thinking about where the areas really were.
  25. *
  26. * Revision 1.8  1993/05/31  20:27:53  richards
  27. * symbols has the whole greek alphabet, but some letters are not named right.
  28. * window.self noww uses two sub-classes of compoundView, one inside the other.
  29. * (Still have problem identifying them...)
  30. * test7 obsolete.
  31. * Fixed some problems with font positioning in boites and poly.
  32. *
  33. * Revision 1.7  1993/05/31  00:12:14  richards
  34. * May 30 checkin. Font support is nearly debugged.
  35. * Make use of glue now.
  36. * Added rational (fraction) types.
  37. *
  38. * Revision 1.6  1993/05/30  21:41:14  richards
  39. * Add rational type.
  40. *
  41. * Revision 1.5  1993/05/23  23:16:47  richards
  42. * No longer puts polynomials under globals, but rather aa.
  43. *
  44. * Revision 1.4  1992/08/19  02:42:21  richards
  45. * Added makeBoite to start the polynomial -> boite conversion. No font support yet.
  46. *
  47. * Revision 1.3  1992/07/05  22:38:37  richards
  48. * Added support for properly parenthesizing infix operators.
  49. * Each infix operator has an infixPriority, and if a lower priority
  50. * operation is printed as part of a higher priority binding (e.g. an
  51. * addition in the middle of a multiplication) then parenthesis are
  52. * added.
  53. *
  54. * Revision 1.2  1992/07/05  22:08:05  richards
  55. * Added and testing scalars and the plus operator.
  56. * The slot `thisObjectPrints' must be on the actual object --- it can't
  57. * be inherited. (This makes sense though...)
  58. *
  59. * Revision 1.1  1992/06/28  15:07:43  richards
  60. * Initial revision
  61. *
  62. *
  63. * /home/2user2/richards/cvs/491/aa/poly.self,v 1.13 1993/07/22 00:15:19 richards Exp 
  64. *
  65. "
  66.  
  67. aa traits _AddSlotsIfAbsent: ( |
  68.     vector = ().
  69.     scalar = ().
  70.     addition = ().
  71.     product = ().
  72.     rational = ().
  73.     exponent = ().
  74.     differentiation = ().
  75.     integration = ().
  76.     operator = ().
  77.     unaryop = (). 
  78.     binop = (). 
  79.     naryop = ()
  80. |)
  81.  
  82. aa mixins _AddSlotsIfAbsent: ( |
  83.     infixop = ().
  84.     poly = ().
  85. |)
  86.  
  87. aa traits operator _Define: (|
  88.     ^ parent* = traits clonable.
  89. |)
  90.  
  91. aa traits vector _Define: (|
  92.     ^ parent* = traits clonable.
  93. |)             
  94.  
  95. aa mixins infixop _Define: (|
  96.     printString: n = ( |output|
  97.     output: ''.
  98.     ( n > infixPriority ) ifTrue: [ output: '(' ].
  99.     doFirst: [ |:firstArg| output: output,
  100.         (firstArg printString: infixPriority) ] 
  101.         Middle:  [ |:anArg|    output: (output,' ',infixSymbol,' ',
  102.             (anArg printString: infixPriority))]
  103.         Last:    [ |:lastArg|  output: (output,' ',infixSymbol,' ',
  104.             (lastArg printString: infixPriority))]
  105.         IfEmpty: [ error: infixSymbol, 
  106.         ' requires at least two arguments'].
  107.     ( n > infixPriority) ifTrue: [ output: output,')'].
  108.     output).
  109.     printString = ( printString: infixPriority ).
  110.  
  111.     makeBoite: n   = ( | myHBoite. |
  112.     myHBoite: aa boites hboite copy.
  113.     myHBoite polyObject: self.
  114.     ( n > infixPriority ) ifTrue: [ myHBoite addEnd: (aa boites htext make: '('). ].
  115.     
  116.     doFirst: [| :firstArg | 
  117.         myHBoite addEnd: firstArg makeBoite: infixPriority.
  118.         myHBoite addEnd: (aa boites htext make: infixSymbol).
  119.     ]
  120.         Middle:  [| :anArg | 
  121.         myHBoite addEnd: anArg makeBoite: infixPriority.
  122.         myHBoite addEnd: (aa boites htext make: infixSymbol).
  123.     ]
  124.         Last:    [| :lastArg| 
  125.         myHBoite addEnd: lastArg makeBoite: infixPriority.
  126.     ]
  127.         IfEmpty: [ error: infixSymbol, 
  128.         ' requires at least two arguments'].
  129.     
  130.     ( n > infixPriority ) ifTrue: [ 
  131.         myHBoite addEnd: (aa boites htext make: ')').
  132.     ].
  133.     myHBoite
  134.     ).
  135.     makeBoite   = ( makeBoite: infixPriority ).
  136.  
  137. |)
  138.  
  139. aa mixins poly _Define: (|
  140.     ^ + aVar = ( |new|
  141.     new: aa prototypes addition copy.
  142.     new buildAddition: self.
  143.     new buildAddition: aVar.
  144.     new
  145.     ).
  146.     ^ * aVar = ( |new|
  147.     new: aa prototypes product copy.
  148.     new buildProduct: self.
  149.     new buildProduct: aVar.
  150.     new
  151.     ).
  152.     ^ / aVar = (| new |
  153.     new: aa prototypes rational copy.
  154.     new numerator: self.
  155.     new denominator: aVar.
  156.     new
  157.     ).
  158.  
  159. |)
  160.  
  161.           
  162. aa traits binop _Define: (|
  163.     ^ parent* = aa traits operator.
  164.     size      = 2.
  165.     doFirst: firstBlock Middle: middleBlock Last: lastBlock IfEmpty: emptyBlock = (
  166.     firstBlock value: leftTerm.
  167.     lastBlock  value: rightTerm.
  168.     ).
  169.     substitute: eqn1 For: eqn2 = (| nb. |
  170.     'substitute: ' print. printLine. eqn1 printLine. eqn2 printLine.
  171.     (equals: eqn2) ifTrue: [ ^(eqn1 copy) ].
  172.     nb: copy.
  173.     nb leftTerm: leftTerm substitute: eqn1 For: eqn2.
  174.     nb rightTerm: rightTerm substitute: eqn1 For: eqn2.
  175.     nb
  176.     ).
  177.     equals: eqn = (
  178.     ((parent = eqn parent) &&                      " MUST BE SAME TYPE "
  179.           [(leftTerm equals: eqn leftTerm) &&
  180.               (rightTerm equals: eqn rightTerm)])
  181.     ).
  182.           
  183. |)
  184.  
  185. aa traits naryop _Define: (|
  186.     ^ parent* = aa traits operator. 
  187.     doFirst: firstBlock Middle: middleBlock Last: lastBlock IfEmpty: emptyBlock = (
  188.     ^(arguments doFirst: firstBlock
  189.         Middle:      middleBlock
  190.         Last:        lastBlock
  191.         IfEmpty:     emptyBlock)
  192.     ).
  193.     substitute: eqn1 For: eqn2 = (| n. |
  194.     'substitute: ' print. printLine. eqn1 printLine. eqn2 printLine.
  195.     (equals: eqn2) ifTrue: [ ^eqn1 copy ].
  196.     n: copy.
  197.     n arguments removeAll.
  198.     arguments do: [|:arg |
  199.         n arguments addLast: (arg substitute: eqn1 For: eqn2).
  200.     ].
  201.     n
  202.     ).
  203.     equals: eqn = (| res. |
  204.     res: (parent = eqn parent) && [arguments size = eqn arguments size].
  205.     res ifTrue: [
  206.         0 to: (arguments size - 1) Do: [|:pos|
  207.         res: res && [(arguments at: pos) equals: (eqn arguments at: pos)].
  208.         ].
  209.     ].
  210.     res
  211.     ).          
  212. |)
  213.  
  214. aa traits addition _Define: (|
  215.     ^ parent** = aa traits naryop.
  216.     ^ infixparent* = aa mixins infixop.
  217.     ^ varparent*   = aa mixins poly.
  218.     infixSymbol = '+'.
  219.     infixPriority = 16.
  220.     arguments = ( terms ).
  221.     copy      = ( |new| 
  222.     new: clone.
  223.     new terms: terms copy.
  224.     new
  225.     ).
  226.     ^ buildAddition: var = (
  227.     terms addLast: var.
  228.     self
  229.     )
  230. |)                              
  231.  
  232. aa traits product _Define: (|
  233.     ^ parent** = aa traits naryop.
  234.     ^ infixparent* = aa mixins infixop.
  235.     ^ varparent*   = aa mixins poly.
  236.     infixSymbol = '*'.
  237.     infixPriority = 32.
  238.     arguments = ( terms ).
  239.     copy      = ( |new| 
  240.     new: clone.
  241.     new terms: terms copy.
  242.     new
  243.     ).
  244.     ^ buildProduct: var = (
  245.     terms addLast: var.
  246.     self
  247.     )
  248. |)                              
  249.  
  250. aa traits rational _Define: (|
  251.     ^ parent**     = aa traits binop.
  252.     ^ infixparent* = aa mixins infixop.
  253.     ^ varparent*   = aa mixins poly.
  254.     infixSymbol    = '/'.
  255.     infixPriority  = 20.
  256.     leftTerm       = ( numerator ).
  257.     leftTerm: n    = ( numerator: n ).
  258.     rightTerm      = ( denominator ).
  259.     rightTerm: n   = ( denominator: n ). 
  260.     copy           = (| new |
  261.     new: clone.
  262.     new
  263.     ).
  264.     makeBoite: n   = ( | vb. hb. line. |
  265.     vb: aa boites vboite copy.       vb polyObject: self.
  266.     hb: aa boites hboite copy.      hb polyObject: numerator.
  267.     
  268.     hb addEnd: aa boites hglue copy.  
  269.     hb addEnd: (numerator makeBoite: infixPriority).
  270.     hb addEnd: aa boites hglue copy.
  271.     vb addEnd: hb.
  272.     
  273.     line: aa boites hline copy.
  274.     vb addEnd: line.
  275.     vb baseLineBoite: line.
  276.     
  277.     hb: aa boites hboite copy.       hb polyObject: denominator.
  278.     hb addEnd: aa boites hglue copy.
  279.     hb addEnd: (denominator makeBoite: infixPriority).
  280.     hb addEnd: aa boites hglue copy.
  281.     vb addEnd: hb.
  282.  
  283.     vb
  284.     ).
  285. |)
  286.         
  287.  
  288. aa traits scalar _Define: (|
  289.     ^ parent** = aa traits vector.
  290.     ^ varparent* = aa mixins poly.
  291.     printString = ( asciiRepresentation ).
  292.     printString: n = ( asciiRepresentation ).
  293.     makeBoite: n = (| nb. |
  294.     nb: boiteRepresentation copy.
  295.     nb polyObject: self.
  296.     nb
  297.     ).
  298.     makeBoite = ( makeBoite: infixPriority ).
  299.     commutes   = true.
  300.     associates = true.
  301.     distributes = true.
  302.     infixPriority = 64.  " () never needed "
  303.     create: name = ( |new|
  304.     new: clone.
  305.     new asciiRepresentation: name.
  306.     new boiteRepresentation: (aa boites htext make: name).
  307.     new
  308.     ).
  309.     create: name Boite: b = ( |new|
  310.     new: clone.
  311.     new asciiRepresentation: name.
  312.     new boiteRepresentation: b.
  313.     new
  314.     ).
  315.     equals: eqn = (
  316.     = eqn
  317.     ).
  318.     substitute: eqn1 For: eqn2 = (
  319.     'substitute: ' print. printLine. eqn1 printLine. eqn2 printLine.
  320.     (equals: eqn2) ifTrue: [ ^eqn1 ].   " SCALARs aren't copied "
  321.     self
  322.     ).
  323. |)
  324.  
  325. " -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- "
  326. "   PROTOTYPES FOR ABOVE OBJECTS  "
  327. " -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- "
  328.  
  329. aa prototypes _AddSlotsIfAbsent: (|
  330.     addition  = ().
  331.     product   = ().
  332.     rational  = ().
  333.     scalar    = ().
  334. |)
  335.  
  336. aa prototypes addition _Define: (|
  337.     ^ parent* = aa traits addition.
  338.     thisObjectPrints = true.
  339.     _^ terms <- list copy.
  340. |)
  341.  
  342. aa prototypes product _Define: (|
  343.     ^ parent* = aa traits product.
  344.     thisObjectPrints = true.
  345.     _^ terms <- list copy.
  346. |)
  347.  
  348. aa prototypes rational _Define: (|
  349.     ^ parent* = aa traits rational.
  350.     thisObjectPrints = true.
  351.     ^ numerator <- nil.
  352.     ^ denominator <- nil.
  353. |)
  354.  
  355. aa prototypes scalar _Define: (|
  356.     ^ parent* = aa traits scalar.
  357.     thisObjectPrints = true.
  358.     _ asciiRepresentation.
  359.     _ boiteRepresentation.
  360. |)
  361.  
  362.  
  363.  
  364.  
  365.