-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOOP1.st
More file actions
162 lines (129 loc) · 3.4 KB
/
OOP1.st
File metadata and controls
162 lines (129 loc) · 3.4 KB
1
Object subclass: #Monom instanceVariableNames: 'exp coef' classVariableNames: '' poolDictionaries: '' category: 'OOP1'!!Monom methodsFor: 'system primitives' stamp: 'AL 4/2/2019 15:00'!coef ^coef.! !!Monom methodsFor: 'system primitives' stamp: 'AL 4/2/2019 15:00'!coef: anInteger coef := anInteger.! !!Monom methodsFor: 'system primitives' stamp: 'AL 4/2/2019 15:24'!derivative | der | der := Monom new. der coef: ( self coef * self exp ). der exp: ( self exp - 1 ). ^der.! !!Monom methodsFor: 'system primitives' stamp: 'AL 4/2/2019 14:54'!exp ^exp.! !!Monom methodsFor: 'system primitives' stamp: 'AL 4/2/2019 15:38'!exp: anInteger (anInteger >= 0) ifTrue: [exp := anInteger] ifFalse: [self error: 'invalid input'].! !!Monom methodsFor: 'system primitives' stamp: 'AL 4/2/2019 15:02'!initialize exp := 0. coef := 0.! !ServiceProvider subclass: #OOP1ServiceProvider instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'OOP1'!"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!OOP1ServiceProvider class instanceVariableNames: ''!!OOP1ServiceProvider class methodsFor: 'initialization' stamp: 'AL 4/2/2019 18:22'!initialize ServiceRegistry buildProvider: self new! !Object subclass: #Polynom instanceVariableNames: 'monoms' classVariableNames: '' poolDictionaries: '' category: 'OOP1'!!Polynom methodsFor: 'system primitives' stamp: 'AL 4/2/2019 18:33'!add: aPolynom |ans monom| (aPolynom isKindOf: Polynom) ifFalse: [self error: 'invalid input'.]. ans := Polynom new. monom := Monom new. monoms keysAndValuesDo: [ :k :v | monom exp: k. monom coef: v. ans addMonom: monom. ]. (aPolynom asDictionary) keysAndValuesDo: [ :k :v | monom exp: k. monom coef: v. ans addMonom: monom. ]. ^ans. ! !!Polynom methodsFor: 'system primitives' stamp: 'AL 4/2/2019 17:21'!addMonom: aMonom |exp coef| (aMonom isKindOf: Monom) ifFalse: [self error: 'invalid input'.]. exp := aMonom exp. coef := aMonom coef. (coef == 0) ifTrue: [^self.]. (monoms includesKey: exp) ifTrue: [monoms at: exp put: (((monoms at: exp) + coef))] ifFalse: [monoms at: exp put: coef]. ((monoms at: exp) == 0) ifTrue: [monoms removeKey: exp]. ! !!Polynom methodsFor: 'system primitives' stamp: 'AL 4/2/2019 16:21'!asDictionary ^monoms.! !!Polynom methodsFor: 'system primitives' stamp: 'AL 4/2/2019 17:51'!derivative | pol m | m := Monom new. pol := Polynom new. monoms keysAndValuesDo: [ :k :v | (k > 0) ifTrue: [ m coef: (k*v). m exp: (k-1). (m coef == 0) ifFalse: [pol addMonom: m]. ] ]. ^pol.! !!Polynom methodsFor: 'system primitives' stamp: 'AL 4/2/2019 18:07'!eval: anInteger |sum| (anInteger isKindOf: Integer) ifFalse: [self error: 'invalid input'.]. sum := 0. monoms keysAndValuesDo: [ :k :v | sum := sum + (v * (anInteger raisedToInteger: k)). ]. ^sum.! !!Polynom methodsFor: 'system primitives' stamp: 'AL 4/2/2019 15:50'!initialize monoms := Dictionary new.! !!Polynom methodsFor: 'system primitives' stamp: 'AL 4/2/2019 17:40'!multiplyByMonom:aMonom |exp coef dict| (aMonom isKindOf: Monom) ifFalse: [self error: 'invalid input'.]. exp := aMonom exp. coef := aMonom coef. dict := Dictionary new. (coef == 0) ifTrue: [monoms := dict. ^self.]. monoms keysAndValuesDo: [ :k :v | dict at: (k+exp) put: (v*coef) ]. monoms := dict.! !OOP1ServiceProvider initialize!