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

Doctests examples for Linear Algebra library as suggested in #18799 #18846

Merged
merged 7 commits into from
Oct 29, 2016
Merged

Doctests examples for Linear Algebra library as suggested in #18799 #18846

merged 7 commits into from
Oct 29, 2016

Conversation

SaschaMann
Copy link
Contributor

Issue #18799

Added Doctests

  • cholfact(A)
  • kron(A, B)
  • pinv(M)
  • nullspace(M)
  • Diagonal(A::AbstractMatrix), Diagonal(V::AbstractVector)
  • norm(v), norm(v, Inf), norm(A, Inf)
  • dot(a, b)
  • inv(M)
  • qr(v)
  • svd(A), svdvals(A)
  • SymTridiagonal(dv, ev), Tridiagonal(dl, d, du), Tridiagonal(A)

Rewritten examples

  • Bidiagonal()
  • Symmetric()
  • Hermitian()

I have rewritten these existing examples to be easier to understand without having to run the code.

make docs && make -C doc doctest runs fine, no failures for stdlib/linalg. The failures it shows are identical to the ones on the master branch.

First PR, so please let me know if I did something wrong :)

Copy link
Member

@Sacha0 Sacha0 left a comment

Choose a reason for hiding this comment

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

@SaschaMann, this is fantastic! A few minor comments on the first half of the PR follow. Thanks and best!

1 ⋅ ⋅ ⋅
7 2 ⋅ ⋅
⋅ 8 3 ⋅
⋅ ⋅ 9 4
Copy link
Member

@Sacha0 Sacha0 Oct 8, 2016

Choose a reason for hiding this comment

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

Perhaps replace #e is with # ev is for consistency with the rest of the docstring?

1 ⋅ ⋅ ⋅
7 2 ⋅ ⋅
⋅ 8 3 ⋅
⋅ ⋅ 9 4
Copy link
Member

Choose a reason for hiding this comment

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

Likewise in this section, perhaps replace #e is with # ev is for consistency with the rest of the docstring?

0 1 0 2
2 0 0 0
0 1 0 0
```
Copy link
Member

@Sacha0 Sacha0 Oct 8, 2016

Choose a reason for hiding this comment

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

Perhaps

julia> A = [1 2; 3 4]
2×2 Array{Int64,2}:
 1  2
 3  4

julia> B = [1 1; 1 1]
2×2 Array{Int64,2}:
 1  1
 1  1

julia> kron(A, B)
4×4 Array{Int64,2}:
 1  1  2  2
 1  1  2  2
 3  3  4  4
 3  3  4  4

would be easier to grasp?

A = diagm(rand(5)) + diagm(rand(4),1); #A is really bidiagonal
factorize(A) #factorize will check to see that A is already factorized
```jldoctest
julia> A = Bidiagonal(ones(5, 5), true) #A is really bidiagonal
Copy link
Member

@Sacha0 Sacha0 Oct 8, 2016

Choose a reason for hiding this comment

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

Calling the Bidiagonal constructor to clarify what's going on is a great idea! The remainder of the example relies on A being an Array though. Perhaps wrap your Bidiagonal constructor call in an Array constructor call (Array(Bidiagonal(ones(5, 5), true)))?

Calling the Bidiagonal constructor makes this example sufficiently clear that the comment may no longer be necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I missed the idea behind the second part of the example. I added the Array constructor to it.

@@ -211,6 +211,25 @@ end
kron(A, B)

Kronecker tensor product of two vectors or two matrices.

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps add an example heading?

@@ -640,6 +689,20 @@ end
nullspace(M)

Basis for nullspace of `M`.

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps add an example heading?

@@ -9,12 +9,38 @@ end
Diagonal(A::AbstractMatrix)

Constructs a matrix from the diagonal of `A`.

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps add an example heading?

"""
Diagonal(A::AbstractMatrix) = Diagonal(diag(A))
"""
Diagonal(V::AbstractVector)

Constructs a matrix with `V` as its diagonal.

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps add an example heading?

@@ -550,6 +574,21 @@ dot(x::Number, y::Number) = vecdot(x, y)
⋅(x,y)

Compute the dot product. For complex vectors, the first vector is conjugated.

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps add an example heading?


julia> dot(a, b)
5
```
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps an example of conjugation in the complex case would be useful, e.g. dot([im, im], [1, 1])?

@SaschaMann
Copy link
Contributor Author

SaschaMann commented Oct 9, 2016

@Sacha0 Most of the already existing examples (both in linalg and in other packages) don't have an example heading, so I intentionally didn't add any. Imo the it's clear from the different formatting and context that something is an example unless there's additional text to it. However, I could add them if you prefer that.

Thanks for your comments! I applied the changes you suggested.

@kshyatt kshyatt added docs This change adds or pertains to documentation linear algebra Linear algebra labels Oct 9, 2016
1 2
3 4

julia> B = [1 1; 1 1]
Copy link
Contributor

Choose a reason for hiding this comment

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

might be more interesting if B isn't all ones here

Copy link
Member

Choose a reason for hiding this comment

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

Apologies, all ones was my suggestion (to make what kron does easy to grasp). Can you think of an equally clear but more exciting example? Best!

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps [im 1; 1 1] would be slightly more interesting but similarly easy to grasp?

Copy link
Contributor

Choose a reason for hiding this comment

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

or in a different spot, but yeah at least one entry not equal to 1.0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll change it to [im 1; 1 -im], that way it's still clear what it does but a bit more interesting.

@Sacha0
Copy link
Member

Sacha0 commented Oct 15, 2016

I applied the changes you suggested.

The changes so far look great!

Most of the already existing examples (both in linalg and in other packages) don't have an example heading, so I intentionally didn't add any. Imo the it's clear from the different formatting and context that something is an example unless there's additional text to it. However, I could add them if you prefer that.

The fifth item in the documentation documentation's list of conventions (about two pages down) suggests segregating examples under an appropriate heading. (Those conventions were codified only a few months ago, so much of the existing documentation does not follow them. #8966 contains additional discussion on evolution of documentation conventions; evolving towards more thorough, structured documentation seems to be the plan.)

That said, as-is is fine by me. Cheers if you prefer to omit example headings, and likewise cheers if you prefer to add them. Whichever stokes your enthusiasm! :)

Best!

@SaschaMann
Copy link
Contributor Author

I just added some more examples.

Also decided to add the example headings to all examples in the file. The reasons you gave for the headings were compelling.

Tests still pass fine:

Document: stdlib/linalg

1 items passed all tests:
177 tests in default
177 tests in 1 items.
177 passed and 0 failed.
Test passed.

Copy link
Member

@Sacha0 Sacha0 left a comment

Choose a reason for hiding this comment

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

Looking great so far! A few minor comments follow below. Thanks!

@@ -161,6 +161,25 @@ end

Compute the Cholesky factorization of a positive definite matrix `A`
and return the UpperTriangular matrix `U` such that `A = U'U`.

**Example**
Copy link
Member

Choose a reason for hiding this comment

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

If memory serves, # Example produces a section heading whereas **Example** produces generic emphasis? (Also below.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, you're right. I should have checked the docs instead of just looking at the existing example headings in the file. My bad, fixed it.

2 50

julia> cholfact!(A)
ERROR: InexactError()
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps expand with an example of valid usage?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cholfact() has an example with proper usage. the description of the !-version just refers to cholfact() as well instead of repeting itself, so imo it makes sense to not create duplicate examples, too. The only addition to the description is the mention of the error, which is what this demonstrates.

Copy link
Member

Choose a reason for hiding this comment

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

👍

4 3
6 3

julia> lufact(A)
Copy link
Member

Choose a reason for hiding this comment

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

Duplicate of the equivalent line just below beginning F = ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Missed that, when copy pasting from the REPL. Fixed it.

@SaschaMann
Copy link
Contributor Author

Changed the Example headings to # Example, removed the duplicate line and added a more interesting example to kron(A, B).

I went through pretty much all the functions in linalg and for the ones that are still without example, I can't come up with anything helpful so I won't add anything after the most recent commit. (Except for fixes or tweaks to what exists of course, if you suggest anything)

@nalimilan
Copy link
Member

Just wanted to note PRs like that are really cool! Glad we have a Sa(s)cha team writing and reviewing this... :-)

Copy link
Member

@Sacha0 Sacha0 left a comment

Choose a reason for hiding this comment

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

@SaschaMann, this is great work! Much thanks!

2 50

julia> cholfact!(A)
ERROR: InexactError()
Copy link
Member

Choose a reason for hiding this comment

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

👍

@andreasnoack
Copy link
Member

@SaschaMann There seems to be a small conflict. Can you please rebase? When that is done I think it can be merged.

@SaschaMann
Copy link
Contributor Author

SaschaMann commented Oct 24, 2016

Rebased and hope this worked properly.

I just saw that in the first commit 5b90333 I added the linalg.rst as well but afterwards I didn't. To make sure that this doesn't cause issues: Does the linalg.rst have to be part of the PR or is it built automatically? Thanks!

@Sacha0
Copy link
Member

Sacha0 commented Oct 24, 2016

Does the linalg.rst have to be part of the PR or is it built automatically?

The PR should include all .rst files that make docs modifies. That said, missed .rst files usually get incorporated into other PRs rapidly, so do not worry overmuch about missed .rst files :).

(Two minor notes for the future[1]:

  1. Suppressing noncritical output via semicolon termination helps keep examples compact. For example, in the Bidiagonal constructor examples, semicolon-terminating the dv and ev definitions (e.g. dv = [1; 2; 3; 4];) would reduce the example's length by almost a factor of two.
  2. Writing vector literals with commas seems more idiomatic than using semicolons. For example, dv = [1, 2, 3, 4] produces a Vector{Int64} (identical to the result of the more general matrix literal dv = [1; 2; 3; 4]).

[1] No need to integrate these suggestions in this already great PR! I would advocate merging this PR as is. Of course if you are enthusiastic about integrating either of these suggestions, please enjoy! I would suggest doing so in followup PRs, smaller PRs being easier to review and merge rapidly.)

Thanks again and best!

@SaschaMann
Copy link
Contributor Author

SaschaMann commented Oct 25, 2016 via email

@Sacha0
Copy link
Member

Sacha0 commented Oct 25, 2016

Barring objections or requests for time, I plan to merge this Wednesday (PDT). Best!

# Example

```jldoctest
julia> A = [1 0 2 0 3; 0 4 0 5 0; 6 0 7 0 8; 0 9 0 1 0; 2 0 3 0 4]
Copy link
Contributor

Choose a reason for hiding this comment

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

probably better to use an example that's complex to make the distinction clearer?

@SaschaMann
Copy link
Contributor Author

Improved the example for Hermitian() as suggested and added the missing .rst file.

@Sacha0 Sacha0 merged commit 9747250 into JuliaLang:master Oct 29, 2016
@Sacha0
Copy link
Member

Sacha0 commented Oct 29, 2016

Thanks for the great work @SaschaMann!

@SaschaMann SaschaMann deleted the smann/lalg-docstrings branch October 29, 2016 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation linear algebra Linear algebra
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants