Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add diag.swift with tests diag_test.swift #44

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Add diag.swift with tests diag_test.swift #44

wants to merge 5 commits into from

Conversation

loseth
Copy link
Collaborator

@loseth loseth commented Aug 7, 2018

Description of Changes

Completed diag.swift and made tests for diag_test.swift.

Resolves

Adds to core functionality.
https://www.pivotaltracker.com/story/show/159221978

Todos

Author:

  • Write up description and link related tickets
  • Ensure your changes include good test coverage
  • Check out our Style Guide
  • Add an approved reviewer
  • Update the ticket status in the relevant tracker (optional)
  • Add benchmark scripts and request benchmarking from reviewer (optional)
  • Respond to requests for changes from the reviewer as needed

Reviewer:

  • Review changes for style, correctness, performance and request changes as necessary.
  • Update the Status document
  • Update the Benchmarks document (optional)
  • Update the ticket status in the relevant tracker
  • Merge into master and change base of any dependent branches
  • Delete this branch

@loseth loseth self-assigned this Aug 7, 2018
@loseth loseth requested a review from philipce August 7, 2018 17:49
/// and zeros everywhere else.
///
/// - Parameter v : A vector data structure
Copy link
Owner

Choose a reason for hiding this comment

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

Extra spaces before colon

let diagonalStartIndex = k >= 0 ? k : length * abs(k)

let vectorIndices = (0..<v.count).map {index in
Copy link
Owner

Choose a reason for hiding this comment

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

My one thought is by precomputing these, we allocate a n size array when we really only need each vector index once. We could pull the data assignment from the for loop below into this loop (and change this loop from a map to a for) and avoid this. Seems like it doesn't matter much though.

public func diag(A: Matrix<Double>, k: Int) -> Vector<Double>
{
precondition(A.rows == A.columns, "Matrix must be square")
Copy link
Owner

Choose a reason for hiding this comment

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

I think we eventually want to move away from using a raw precondition or assert to use the wrappers warn and error. I was hoping that the wrappers would help in testing but I kind of got stuck. We don't have to change this now, but perhaps it's a good time to have a conversation about this; I'd love to pick your brain

{
precondition(A.rows == A.columns, "Matrix must be square")
precondition(k < abs(A.rows), "abs(k) must be less than the the A dimensions")
Copy link
Owner

Choose a reason for hiding this comment

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

I believe the matrix verifies rows are always positive so the abs is unnecessary

data.removeSubrange(rangeLower)

let indices = data.indices.filter {$0 % interval == 0}
Copy link
Owner

Choose a reason for hiding this comment

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

Clever! The only concern is that according to the removeSubrange docs: the complexity is "O(n), where n is the length of the collection." So this it's potentially expensive to slice off the ends. What if instead in the filter we add on a bounds check that the element is between the start and end index? And I guess we'd have to add an offset for the mod operation too...

let tests =
[
testCase([("testBasic", self.testBasic)]),
]
Copy link
Owner

Choose a reason for hiding this comment

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

Looks like some whitespace weirdness

// TODO: fill me in
print("\n\t*** WARNING: Test unimplemented - \(#file)\n")

func testBasic()
Copy link
Owner

Choose a reason for hiding this comment

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

Very nice! Some time let's talk about strategies for testing, whether the testBasic pattern I started with is really what we want to do or whether smaller test cases are better.

@philipce
Copy link
Owner

philipce commented Aug 9, 2018

Very clean and readable! Nice! Left a few comments. We don't have to change anything necessarily but let's discuss before it goes in

@loseth
Copy link
Collaborator Author

loseth commented Aug 9, 2018

Great feedback! I'll have a look at it in the weekend, and try to avoid the obvious typos next time.
Do you want the discussions at Slack or at Pivotal?

@philipce
Copy link
Owner

Well it was very nice code to review! As far as discussions go I think discussions specific to a ticket make sense in tracker. But I think both these questions are larger so slack makes sense? What do you think?

@loseth
Copy link
Collaborator Author

loseth commented Aug 10, 2018

I think we can do the broad discussions in Slack (make some more channels?) and then make some summaries/conclusions in Tracker afterwards?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants