Skip to content

Conversation

@rakki-18
Copy link
Contributor

A raisedTo: anInteger method is added for PMMatrix. Now, exponentiation of matrices can be performed using this method.

Eg: You can calculate A3 using A raisedTo: 3.

A raisedTo: anInteger method is added for PMMatrix. Now, exponentiation of matrices can be performed using this method.
self assert: self numberOfRows = self numberOfColumns description: 'Matrix should be square'.

anInteger <= 0 ifTrue: [
anInteger = 0 ifTrue: [^ PMMatrix identity: self numberOfRows ]
Copy link
Contributor

@hemalvarambhia hemalvarambhia Jul 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested ifs can impact comprehensibility. Perhaps, makes this a guard clause all on its own for a power = 0. Assuming you've not considered this already, is there a way we may avoid chains ifTrue/ifFalse, as I personally am finding it hard to follow

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hemalvarambhia I have reformatted it now. Can you please check it?

]

{ #category : #operation }
PMMatrix >> raisedTo: anInteger [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PMMatrix >> raisedTo: anInteger [
PMMatrix >> raisedTo: power [

Copy link
Contributor

@hemalvarambhia hemalvarambhia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will approve once my suggestions have been addressed. I would prefer the selector argument was renamed to power (index is another commonly used word 'power of indicies') and for there to be fewer nested ifs, as it would help me with my need to understand the code easily. The rest are formatting suggestions

m printOn: stream
]

{ #category : #tests }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are great tests! Well done. My only suggestions are formatting to Arrange Act Assert.

aPMMatrix := PMMatrix rows: #(#(3 1) #(1 1)).

aPMMatrix := aPMMatrix raisedTo: -2.
expected := PMMatrix rows: #(#(0.5 -1) #(-1 2.5)).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the expected to be close to the self assert...

aPMMatrix := PMMatrix rows: #(#(3 1) #(1 1)).

aPMMatrix := aPMMatrix raisedTo: 3.
expected := PMMatrix rows: #(#(34 14) #(14 6)).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion here - move the expected closer to self assert ...

aPMMatrix := PMMatrix rows: #(#(3 1) #(1 1)).

aPMMatrix := aPMMatrix raisedTo: 0.
expected := aPMMatrix.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion here.

Some formatting changes, renaming arguments are also done
@rakki-18
Copy link
Contributor Author

@hemalvarambhia I have made the modifications. Can you please review it when you are free?

Copy link
Contributor

@hemalvarambhia hemalvarambhia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See if you can appeal to iteration over recursion to speed things up

^ PMMatrix identity: self numberOfRows ].

aPower > 0 ifTrue: [
^ self * (self raisedTo: aPower -1) ].
Copy link
Contributor

@hemalvarambhia hemalvarambhia Jul 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recursion! Is there a way to iterate instead in order to make it faster? I misread this due to a lack of space between the '-' and '1'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hemalvarambhia Yes, I could change the recursion to iteration. I had kept recursion because I felt that the code looked more concise and elegant.

Copy link
Contributor

@hemalvarambhia hemalvarambhia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. Worth investigating iteration over recursion for speed.

@rakki-18
Copy link
Contributor Author

Approved. Worth investigating iteration over recursion for speed.

@hemalvarambhia I have replaced recursion with iteration now.

@hemalvarambhia
Copy link
Contributor

@rakki-18 great job. It's great you did it so quickly. It's approved so we'll merge soon...

@rakki-18
Copy link
Contributor Author

@rakki-18 great job. It's great you did it so quickly. It's approved so we'll merge soon...

Thanks a lot!

@hemalvarambhia hemalvarambhia merged commit 52694cb into PolyMathOrg:master Jul 17, 2021
@SergeStinckwich SergeStinckwich added this to the v1.0.4 milestone Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants