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

added hcat/vcat/hvcat methods for UniformScaling #19305

Merged
merged 3 commits into from
Nov 16, 2016

Conversation

stevengj
Copy link
Member

@stevengj stevengj commented Nov 11, 2016

It is nice to be able to construct matrices with identity components using I, where the size of the identity is inferred from the other pieces of the matrix. e.g. with this PR you can now do:

julia> [rand(3,3) I; 2I 3I; 4I]
12×6 Array{Float64,2}:
 0.0206989  0.703627   0.582238  1.0  0.0  0.0
 0.993276   0.0728296  0.900664  0.0  1.0  0.0
 0.424733   0.578488   0.081822  0.0  0.0  1.0
 2.0        0.0        0.0       3.0  0.0  0.0
 0.0        2.0        0.0       0.0  3.0  0.0
 0.0        0.0        2.0       0.0  0.0  3.0
 4.0        0.0        0.0       0.0  0.0  0.0
 0.0        4.0        0.0       0.0  0.0  0.0
 0.0        0.0        4.0       0.0  0.0  0.0
 0.0        0.0        0.0       4.0  0.0  0.0
 0.0        0.0        0.0       0.0  4.0  0.0
 0.0        0.0        0.0       0.0  0.0  4.0

For sparse matrices, it concatenates a sparse identity efficiently. I also added a sparse(I, m,n) constructor since this seemed generally useful.

This change is technically breaking because hcat(I, I) now throws an error (because it can't figure out the size of the resulting matrix), whereas before it returned a 1×2 Array{UniformScaling{Int64},2}. I doubt this will affect many people.

@stevengj stevengj added breaking This change will break code linear algebra Linear algebra labels Nov 11, 2016
@StefanKarpinski
Copy link
Member

I like it, but of course, this leads to wanting Ones and Zeros objects that behave similarly.

@stevengj
Copy link
Member Author

stevengj commented Nov 11, 2016

@StefanKarpinski, you can get a zeros object just by 0I. I'm skeptical of the need for a Ones object (corresponding to an implicit-size ones?) in this sort of matrix construction.

@stevengj
Copy link
Member Author

stevengj commented Nov 11, 2016

PS. I would have used @inferred in the tests, but ran into #19304.

@StefanKarpinski
Copy link
Member

If UniformScaling were generalized to have a base value and a diagonal value, then we could make things like 0*I+1 work. I'm not sure if that's something we should do, but it's worth considering.

@andreasnoack
Copy link
Member

Great. I think this will be really useful.

@stevengj
Copy link
Member Author

Matrices full of constants just aren't that useful in linear algebra, so I doubt it is worth it. (This is similar to the discussion in #14963 about whether we should implement "sparse" matrices with a nonzero "default" value.)

@StefanKarpinski
Copy link
Member

I dunno, I've written a lot of code concatenating ones(m,n) with other matrices. Often m or n is 1 but not always. I actually think that having sparse matrices with non-zero default is a good idea since it makes many operations on sparse matrices closed. The main drawback is the complexity of the implementation. In this case the implementation is pretty simple.

@stevengj
Copy link
Member Author

Getting

    FAILURE
Error in testset spawn:
Test Failed
  Expression: false

on Linux, which seems unrelated. Is there a problem with Travis?

@stevengj
Copy link
Member Author

General low-rank updates would be useful, but they vastly complicate the implementation; low-rank matrices are really their own data structure (e.g. for BFGS updates). Adding a constant everywhere is just a very special case of a rank-1 update, and in my opinion is not worth the trouble (every single sparse-matrix routine everywhere would have to be updated, and unexpectedly so — people writing "sparse" code would not expect this definition of "sparse"), and SuiteSparse would be a hassle. In any case, this seems like something to be discussed in its own issue.

@stevengj
Copy link
Member Author

Restarting CI now that #19132 is closed.

@stevengj
Copy link
Member Author

Yay, CI passes again. Okay to merge, since it seems that supporting this functionality is uncontroversial?

@andreasnoack andreasnoack merged commit 832ab88 into JuliaLang:master Nov 16, 2016
@stevengj stevengj deleted the Icat branch November 16, 2016 20:59
@tkelman
Copy link
Contributor

tkelman commented Nov 16, 2016

should have run nanosoldier here, make sure this doesn't slow down other concatenation cases

@stevengj
Copy link
Member Author

How could it? The new code is only called when you have a UniformScaling argument.

@tkelman
Copy link
Contributor

tkelman commented Nov 17, 2016

it's called on a union, and a similar change with sparse matrices caused a big regression several months ago

@andreasnoack
Copy link
Member

I'm not sure if you two noticed 832ab88#commitcomment-19855129.

@stevengj
Copy link
Member Author

stevengj commented Nov 17, 2016

Thanks @andreasnoack. Also, @tkelman, in this case it is defined for Union{AbstractVecOrMat,UniformScaling}... arguments, but there is already an hcat method (etc.) defined for AbstractVecOrMat..., so as far as I can tell it is not possible to call the new hcat method unless you pass a UniformScaling argument. If you pass only arrays, it will always call the old AbstractVecOrMat... method.

@tkelman
Copy link
Contributor

tkelman commented Nov 17, 2016

does look like this caused a stack overflow in ImageFiltering.jl's tests http://pkg.julialang.org/logs/ImageFiltering_0.6.log

@stevengj
Copy link
Member Author

Ah, looks like we need an hvcat(::Tuple{Vararg{Int}}, ::AbstractVecOrMat...) method to disambiguate.

@stevengj
Copy link
Member Author

stevengj commented Nov 17, 2016

A similar example that overflows (via a dispatch loop) is [(1:2) zeros(2,2); ones(3,3)]

stevengj added a commit to stevengj/julia that referenced this pull request Nov 17, 2016
stevengj added a commit that referenced this pull request Nov 21, 2016
fcard pushed a commit to fcard/julia that referenced this pull request Feb 28, 2017
* added hcat/vcat/hvcat methods for UniformScaling

* NEWS for 19305

* unified speye and sparse(I) code, and added a note about the latter in the docs of the former
fcard pushed a commit to fcard/julia that referenced this pull request Feb 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking This change will break code linear algebra Linear algebra
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants