Skip to content

Added setindex! for ColVecs and RowVecs with tests #196

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

Merged
merged 3 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "KernelFunctions"
uuid = "ec8451be-7e33-11e9-00cf-bbf324bd1392"
version = "0.8.7"
version = "0.8.8"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
13 changes: 2 additions & 11 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Base.size(D::ColVecs) = (size(D.X, 2),)
Base.getindex(D::ColVecs, i::Int) = view(D.X, :, i)
Base.getindex(D::ColVecs, i::CartesianIndex{1}) = view(D.X, :, i)
Base.getindex(D::ColVecs, i) = ColVecs(view(D.X, :, i))
Base.setindex!(D::ColVecs, v::AbstractVector, i) = setindex!(D.X, v, :, i)

dim(x::ColVecs) = size(x.X, 1)

Expand Down Expand Up @@ -76,6 +77,7 @@ Base.size(D::RowVecs) = (size(D.X, 1),)
Base.getindex(D::RowVecs, i::Int) = view(D.X, i, :)
Base.getindex(D::RowVecs, i::CartesianIndex{1}) = view(D.X, i, :)
Base.getindex(D::RowVecs, i) = RowVecs(view(D.X, i, :))
Base.setindex!(D::RowVecs, v::AbstractVector, i) = setindex!(D.X, v, i, :)

dim(x::RowVecs) = size(x.X, 2)

Expand All @@ -94,17 +96,6 @@ function pairwise!(out::AbstractMatrix, d::PreMetric, x::RowVecs, y::RowVecs)
return Distances.pairwise!(out, d, x.X, y.X; dims=1)
end

"""
Will be implemented at some point
```julia
params(k::Kernel)
params(t::Transform)
```
For a kernel return a tuple with parameters of the transform followed by the specific parameters of the kernel
For a transform return its parameters, for a `ChainTransform` return a vector of `params(t)`.
"""
#params

dim(x) = 0 # This is the passes-by-default choice. For a proper check, implement `KernelFunctions.dim` for your datatype.
dim(x::AbstractVector) = dim(first(x))
dim(x::AbstractVector{<:AbstractVector{<:Real}}) = length(first(x))
Expand Down
12 changes: 11 additions & 1 deletion test/utils.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
@testset "utils" begin
using KernelFunctions: vec_of_vecs, ColVecs, RowVecs
rng, N, D = MersenneTwister(123456), 10, 4
x, X = randn(rng, N), randn(rng, D, N)
x = randn(rng, N)
X = randn(rng, D, N)
v = randn(rng, D)
w = randn(rng, N)

@testset "VecOfVecs" begin
@test vec_of_vecs(X, obsdim = 2) == ColVecs(X)
@test vec_of_vecs(X, obsdim = 1) == RowVecs(X)
Expand All @@ -19,6 +23,9 @@
@test getindex(DX, :) == ColVecs(X)
@test eachindex(DX) == 1:N
@test first(DX) == X[:, 1]
DX[2] = v
@test DX[2] == v
@test X[:, 2] == v

Y = randn(rng, D, N + 1)
DY = ColVecs(Y)
Expand Down Expand Up @@ -53,6 +60,9 @@
@test getindex(DX, :) == RowVecs(X)
@test eachindex(DX) == 1:D
@test first(DX) == X[1, :]
DX[2] = w
@test DX[2] == w
@test X[2, :] == w

Y = randn(rng, D + 1, N)
DY = RowVecs(Y)
Expand Down