Skip to content

Instantly share code, notes, and snippets.

@Sacha0
Last active November 9, 2017 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sacha0/0a677f941b6374d5fb5573f35a6bd8f6 to your computer and use it in GitHub Desktop.
Save Sacha0/0a677f941b6374d5fb5573f35a6bd8f6 to your computer and use it in GitHub Desktop.
ones in the wild
# rewrites mostly preserve surrounding style.
# sometimes I've added whitespace for sanity.
# several of these would be better rewritten more extensively,
# but instead I've mostly just transliterated ones to something more appropriate.
# orig
p = poly([ones(1,4*k),2*ones(1,3*k),3*ones(1,2*k),4*ones(1,k)]);
# rewrite
p = poly([fill(1.,1,4k),fill(2.,1,3k),fill(3.,1,2k),fill(4.,1,k)]);
# summary
ad hoc fill 3
ones, fill longer 1
# orig
"coverage" => Array[
4 [2*ones(Int,8)],
5 [2*ones(Int,8)],
6 [zeros(Int,8)],
7 [2*ones(Int,8)],
...
17 "availability" => Array[
18 [ones(Int,8)],
19 [ones(Int,8)],
20 [ones(Int,8)],
# rewrite
"coverage" => Array[
4 [fill(2,8)],
5 [fill(2,8)],
6 [fill(0,8)],
7 [fill(2,8)],
...
17 "availability" => Array[
18 [fill(1,8)],
19 [fill(1,8)],
20 [fill(1,8)],
# summary
ad hoc fill 3
ones, fill shorter 3
zeros, fill shorter 1
# orig
p = poly([-0.2*ones(1,k),0.39*ones(1,k),0.40*ones(1,k)])
# rewrite
p = poly([fill(-0.2,1,k),fill(0.39,1,k),fill(0.40,1,k)])
# summary
ad hoc fill 3
# orig
function unit_rhs(x, y, nel)
2 f = ones(nel, 1)
# rewrite
2 f = fill(1., nel, 1)
# summary
ones, fill longer 1
# orig
A = ones(Int, 8,8)
# rewrite
A = fill(1, 8,8)
# summary
ones, fill shorter 1
# orig
6 A = [[ones(Int, 4,4) 2*ones(Int, 4,4)]; [3*ones(Int, 4,4) 4*ones(Int, 4,4)]]
# rewrite
6 A = [[fill(1, 4,4) fill(2, 4,4)]; [fill(3, 4,4) fill(4, 4,4)]]
# summary
ad hoc fill 3
ones, fill shorter 1
# orig
"coverage" => Array[
# Monday
[3, 4, 5 * ones(Int, 6), 4 * ones(Int, 2), 2 * ones(Int, 2)],
# Tuesday
[3, 4, 5 * ones(Int, 6), 4 * ones(Int, 2), 2 * ones(Int, 2)],
# Wednesday
[3, 4, 5 * ones(Int, 6), 4 * ones(Int, 2), 2 * ones(Int, 2)],
# Thursday
[3, 4, 5 * ones(Int, 6), 4 * ones(Int, 2), 2 * ones(Int, 2)],
# Friday
[3, 4, 5 * ones(Int, 6), 4 * ones(Int, 2), 2 * ones(Int, 2)],
# Saturday
[1, 2 * ones(Int, 11)],
# Sunday
[1, 2 * ones(Int, 11)],
# rewrite
"coverage" => Array[
# Monday
[3, 4, fill(5, 6), fill(4, 2), fill(2, 2)],
# Tuesday
[3, 4, fill(5, 6), fill(4, 2), fill(2, 2)],
# Wednesday
[3, 4, fill(5, 6), fill(4, 2), fill(2, 2)],
# Thursday
[3, 4, fill(5, 6), fill(4, 2), fill(2, 2)],
# Friday
[3, 4, fill(5, 6), fill(4, 2), fill(2, 2)],
# Saturday
[1, fill(2, 11)],
# Sunday
[1, fill(2, 11)],
# summary
ad hoc fill 17
# orig
5 ShapleyValues.addsample!(s, ones(4))
6 @test ShapleyValues.data(s) == ones(4,1)
...
9 ShapleyValues.addsample!(s, ones(4))
10 ShapleyValues.addsample!(s, ones(4))
# rewrite
5 ShapleyValues.addsample!(s, fill(1.,4))
6 @test ShapleyValues.data(s) == fill(1.,4,1)
...
9 ShapleyValues.addsample!(s, fill(1.,4))
10 ShapleyValues.addsample!(s, fill(1.,4))
# summary
ones, fill longer 4
# orig
3 σ = 0.1 * ones(n)
4 μ = 0.0027 * ones(n)
# rewrite
3 σ = fill(0.1, n)
4 μ = fill(0.0027, n)
# summary
ad hoc fill 2
# orig
5 out=(a*ones(1,size(B,2))).*B
# rewrite
5 out=fill(a,1,size(B,2)).*B
# summary
ad hoc fill 1
# NOTE! a is scalar
# orig
p = [-1,ones(1,n)];p
# rewrite
p = [-1,fill(1.,1,n)];p
# summary
ones, fill longer 1
# orig
A1 = Lineage(name = "antibody lin 1", n = 500, s = 1, kappa = ones(25),
3 shat = 1, kappahat = ones(25), randclone = randclone_1)
# rewrite
A1 = Lineage(name = "antibody lin 1", n = 500, s = 1, kappa = fill(1.,25),
3 shat = 1, kappahat = fill(1.,25), randclone = randclone_1)
# summary
ones, fill longer 2
# orig
@test juliaset.createjuliaset(ones(1),zeros(1),10,100,0) == 11*ones(1,1)
# rewrite
@test juliaset.createjuliaset([1.],[0.],10,100,0) == fill(11.,1,1)
# summary
ad hoc fill 1
ad hoc vector literal 2
# orig
2 "coverage" => [3, 4, 5 * ones(Int, 6), 4 * ones(Int, 2), 2 * ones(Int, 2)],
12 "availability" => ones(Int, 12),
# rewrite
2 "coverage" => [3, 4, fill(5, 6), fill(4, 2), fill(2, 2)],
12 "availability" => fill(1, 12),
# summary
ad hoc fill 3
ones, fill shorter 1
# orig
I = 15
...
Z1 = [[zeros(1); ones(I)] zeros(I+1) -next_i(0:I, 1);
[zeros(1); ones(I)] zeros(I+1) -next_i(0:I, 1);
[zeros(1); ones(I)] zeros(I+1) -next_i(0:I, 1)]
Z2 = [ones(3*(I+1)) -[p[1]*n[2]*ones(I+1);p[2]*n[2]*ones(I+1);p[3]*n[2]*ones(I+1)] -kron(ones(3), next_i(0:I, 2))]
Z3 = [ones(3*(I+1)) -[p[1]*n[3]*ones(I+1);p[2]*n[3]*ones(I+1);p[3]*n[3]*ones(I+1)] -kron(ones(3), next_i(0:I, 3))]
# rewrite
I = 15
...
Z1 = [[[0.]; fill(1,I)] zeros(I+1) -next_i(0:I, 1);
[[0.]; fill(1,I)] zeros(I+1) -next_i(0:I, 1);
[[0.]; fill(1,I)] zeros(I+1) -next_i(0:I, 1)]
Z2 = [fill(1., 3*(I+1)) [fill(-p[1]*n[2], I+1); fill(-p[2]*n[2], I+1); fill(-p[3]*n[2], I+1)] kron(fill(-1.,3), next_i(0:I, 2))]
Z3 = [fill(1., 3*(I+1)) [fill(-p[1]*n[3], I+1); fill(-p[2]*n[3], I+1); fill(-p[3]*n[3], I+1)] kron(fill(-1.,3), next_i(0:I, 3))]
# summary
ad hoc fill 8
ones, fill longer 5
# NOTE! added some whitespace for sanity
# orig
X = 0.17981663628808964; # mean Zm off Spain
...
8 bio_pisc = ones(PRM_PI.N) .* X
9 bio_plan = ones(PRM_PL.N) .* X
10 bio_detr = ones(PRM_DE.N) .* X
11 bio_W = ones(1) .* X;
# rewrite
X = 0.17981663628808964; # mean Zm off Spain
...
8 bio_pisc = fill(X, PRM_PI.N)
9 bio_plan = fill(X, PRM_PL.N)
10 bio_detr = fill(X, PRM_DE.N)
11 bio_W = fill(X, 1); # or [X]
# summary
ad hoc fill 4 (or with 1 vector literal)
# orig
res = J([ones(m) X], ones(2), 1.0)
36 jdev(X, θ, λ, j) = (1/m) * sum((X * θ .- y) .* X[:, j]) + (j == 1 ? 0 : (λ/m * θ[j]))
38 @test jdev([ones(m) X], ones(2), 1, 1) - (-15.303016) < 1e-6
# rewrite
res = J([fill(1., m) X], fill(1., 2), 1.0)
36 jdev(X, θ, λ, j) = (1/m) * sum((X * θ .- y) .* X[:, j]) + (j == 1 ? 0 : (λ/m * θ[j]))
38 @test jdev([fill(1., m) X], fill(1., 2), 1, 1) - (-15.303016) < 1e-6
# or
res = J([fill(1., m) X], [1., 1.], 1.0)
36 jdev(X, θ, λ, j) = (1/m) * sum((X * θ .- y) .* X[:, j]) + (j == 1 ? 0 : (λ/m * θ[j]))
38 @test jdev([fill(1., m) X], [1., 1.], 1, 1) - (-15.303016) < 1e-6
# summary
could say: ones, fill longer 4
or could say: ones, fill longer 2 + two vector literals
# orig
13 l = [ kron(0.2, ones(1, J*J*s)) kron(-2.5, ones(1,n*J*s)) kron(0.4, ones(1,n)) kron(0.9, ones(1,s)) ]'
14 u = [ kron(0.997, ones(1, J*J*s)) kron(2.5, ones(1,n*J*s)) kron(1.0, ones(1,n)) kron(0.999, ones(1,s)) ]'
# rewrite
13 l = [ fill(0.2, 1,J*J*s) fill(-2.5, 1,n*J*s) fill(0.4, 1,n) fill(0.9, 1,s) ]'
14 u = [ fill(0.997, 1, J*J*s) fill(2.5, 1,n*J*s) fill(1.0, 1,n) fill(0.999, 1,s) ]'
# summary
ad hoc fill 8
# orig
93 DE_lambda = ones(DE_N) .* 0.7;
94 PI_lambda = ones(PI_N) .* 0.7;
95 PL_lambda = ones(PL_N) .* 0.7;
# rewrite
93 DE_lambda = fill(0.7, DE_N);
94 PI_lambda = fill(0.7, PI_N);
95 PL_lambda = fill(0.7, PL_N);
# summary
ad hoc fill 3
# orig
63 b = ones(n)
64 a = zeros(n)
65
66 # solve (T + H)a = b
67 theq!(a, t, h, b)
68 @test a ≈ ones(n)
# rewrite
63 b = fill(1., n)
64 a = zeros(n)
65
66 # solve (T + H)a = b
67 theq!(a, t, h, b)
68 @test a ≈ fill(1., n)
# summary
ones, fill longer 2
# orig
p = poly([i*ones(1,5),-i*ones(1,5),0.5i*ones(1,4), -0.5i*ones(1,4),0.75i,-0.75i]);
# rewrite
p = poly([fill(i,1,5),fill(-i,1,5),fill(0.5i,1,4), fill(-0.5i,1,4),0.75i,-0.75i]);
# summary
ad hoc fill 4
# orig
A[j, 1:n] = K[j] == 0 ? ones(n)/n : A[j, 1:n] / K[j]
# rewrite
A[j, 1:n] = K[j] == 0 ? fill(1/n, n) : A[j, 1:n] / K[j]
# summary
ad hoc fill 1
# orig
p = poly([ones(1,l),2*ones(1,m),3*ones(1,n)]);
# rewrite
p = poly([fill(1.,1,l),fill(2.,1,m),fill(3.,1,n)]);
# summary
ad hoc fill 2
ones, fill longer 1
# orig
3 @test_approx_eq matrixdepot("circul", ones(Float64,n)) ones(Float64, n, n)
# rewrite
3 @test_approx_eq matrixdepot("circul", fill(1., n)) fill(1., n, n)
# summary
ones, fill shorter 2
# orig
5 @test istril(A - ones(n,n))
6 @test istriu(A + diagm(ones(n-1), -1))
# rewrite
5 @test istril(A .- 1.)
6 @test istriu(A + diagm(fill(1.,n-1), -1))
# summary
ad hoc broadcast subtraction 1
ones, fill longer 1
# orig
@testset "rankUpdate" begin
5 @test ones(2, 2) == MixedModels.rankUpdate!(1.0, ones(2), Hermitian(zeros(2, 2)))
# rewrite
@testset "rankUpdate" begin
5 @test fill(1, 2, 2) == MixedModels.rankUpdate!(1.0, fill(1., 2), Hermitian(zeros(2, 2)))
# summary
ones, fill longer 2
# orig
function pdist(X::Matrix, Y::Matrix)
m = size(X,1);
n = size(Y,1);
Yt = Y';
XX = sum(X.*X,2);
YY = sum(Yt.*Yt,1);
D = XX[:,ones(n)] + YY[ones(m), :] - 2*X*Yt;
return D
end
# rewrite
# this actually isn't valid code anymore, as indexing with floats is not possible
# could rewrite naively with fill, but really this is an ad hoc broadcast
function pdist(X::Matrix, Y::Matrix)
m = size(X,1);
n = size(Y,1);
Yt = Y';
XX = sum(X.*X,2);
YY = sum(Yt.*Yt,1);
D = XX .+ YY .- 2.*X*Yt;
return D
end
# summary
ad hoc broadcast 2
# orig
msh = getTensorMesh3D(ones(3), ones(3), ones(3))
18 Ainv = getJuliaSolver()
20 pf = TestForwardProbType(msh,ones(3),ones(3),ones(3),Ainv);
# rewrite
msh = getTensorMesh3D(fill(1., 3), fill(1., 3), fill(1., 3))
18 Ainv = getJuliaSolver()
20 pf = TestForwardProbType(msh,fill(1., 3),fill(1., 3),fill(1., 3),Ainv);
# or with names
x = fill(1., 3)
msh = getTensorMesh3D(x, x, x)
18 Ainv = getJuliaSolver()
20 pf = TestForwardProbType(msh,x,x,x,Ainv);
# summary
ones, fill longer 6 (static length, not mutated, all same, can rename)
# orig
bio = ones(GRD["N"],PRM_PI.N) .* eps()
13 met = ones(GRD["N"],PRM_PI.N) .* eps()
14 tau = ones(GRD["N"],PRM_PI.N) .* eps()
18 bio = ones(GRD["N"],PRM_PL.N) .* eps()
# rewrite
bio = fill(eps(), GRD["N"], PRM_PI.N)
13 met = fill(eps(), GRD["N"], PRM_PI.N)
14 tau = fill(eps(), GRD["N"], PRM_PI.N)
18 bio = fill(eps(), GRD["N"], PRM_PL.N)
# summary
ad hoc fill 4
# orig
A = SymTridiagonal(2*ones(n), -ones(n-1))
# rewrite
A = SymTridiagonal(fill(2., n), fill(-1., n-1))
# summary
ad hoc fill 2
# orig
6 g(x) = ones(n)*(dot(x,x)+1);
9 x0 = ones(n);
11 (exitflag, x, fx, gx, k) = NewtonISMP.solve(f, g, H, x0);
# rewrite
6 g(x) = fill(dot(x,x)+1, n);
9 x0 = fill(1., n);
11 (exitflag, x, fx, gx, k) = NewtonISMP.solve(f, g, H, x0);
# summary
ad hoc fill 1
ones, fill longer 1
# orig
7 @test convert(Elemental.DistMatrix{T}, ones(T, 5)) == ones(5,1)
# rewrite
7 @test convert(Elemental.DistMatrix{T}, fill(T(1), 5)) == fill(1.,5,1)
# summary
ones, fill longer 2
# orig
wb2 = FileBackedBuffer(f2, "B", Float64, (2,), 5)
9 @test wb.bsize == 0
10 @test size(wb.A) == (3,4,0)
11 push!(wb, ones(3,4))
12 @test wb.bsize == 1
13 @test size(wb.A) == (3,4,0)
14 for i=1:3 push!(wb, ones(3,4)) end
15 @test wb.bsize == 4
# rewrite
wb2 = FileBackedBuffer(f2, "B", Float64, (2,), 5)
9 @test wb.bsize == 0
10 @test size(wb.A) == (3,4,0)
11 push!(wb, fill(1.,3,4))
12 @test wb.bsize == 1
13 @test size(wb.A) == (3,4,0)
14 for i=1:3 push!(wb, fill(1.,3,4)) end
15 @test wb.bsize == 4
# summary
ones, fill longer 2
# orig
6 X = ones(30,1)
8 @test_throws ArgumentError LinearMixedModel(mf, ones(1), Λ, X, y, Float64[])
# rewrite
6 X = fill(1.,30,1)
8 @test_throws ArgumentError LinearMixedModel(mf, [1.], Λ, X, y, Float64[])
# summary
ad hoc vector literal 1
ones, fill longer 1
# orig
function sum(x::AffineExpr)
5 return ones(1, x.size[1]) * x * ones(x.size[2], 1)
6 end
7
8 function sum(x::AffineExpr, dim::Int64)
9 if dim == 1
10 return ones(1, x.size[1]) * x
# rewrite
function sum(x::AffineExpr)
5 return fill(1., 1, x.size[1]) * x * fill(1., x.size[2], 1)
6 end
7
8 function sum(x::AffineExpr, dim::Int64)
9 if dim == 1
10 return fill(1., 1, x.size[1]) * x
# summary
ones, fill longer 3
# orig
2 x = ones(Int, N+1, N+1)
# rewrite
2 x = fill(1, N+1, N+1)
# summary
ones, fill shorter 1
# orig
function alloc(N::Int)
2 xs = ones(N,N,N)
3 x = maximum(xs)
4 ys = ones(N,N,N)
5 y = maximum(ys)
6 end
7 @time alloc(200)
# not real code, next line is @time alloc(...), benchmark play, ignoring
# orig
7 x=[x ones(size(data,1),1)]
# rewrite
7 x=[x fill(1.,size(data,1),1)]
# summary
ones, fill longer 1
# orig
@test evaluate(r, ones(7)) == 0
9 @test evaluate(r, ones(100)) == Inf
10 @test_approx_eq prox(r, ones(100), 1) ones(100)/sqrt(100)*7
# rewrite
@test evaluate(r, fill(1., 7)) == 0
9 @test evaluate(r, fill(1., 100)) == Inf
10 @test_approx_eq prox(r, fill(1., 100), 1) fill(7/sqrt(100), 100)
# summary
ad hoc fill 1
ones, fill longer 3
# orig
kernel = ones(eltype(A),L)/L;
# rewrite
kernel = fill(one(eltype(A))/L, L);
# summary
ad hoc fill 1
# orig
@test B[1,:][:] == ones(n)
# rewrite
@test B[1,:][:] == fill(1., n)
# summary
ones, fill longer 1
# orig
X0 = BallInf(ones(4), 0.1)
# rewrite
X0 = BallInf(fill(1., 4), 0.1)
# summary
ones, fill longer 1
# orig
g = ProxAlgs.BoxConstraint(ones(3),2*ones(3))
# rewrite
g = ProxAlgs.BoxConstraint(fill(1., 3), fill(2., 3))
# summary
ad hoc fill 1
ones, fill longer 1
# orig
5 c = ones(Int, n)
# rewrite
5 c = fill(1, n)
# summary
ones, fill shorter 1
# orig
9 nlp=ADNLPModel(f,ones(2), lvar=-Inf*ones(2), uvar=Inf*ones(2), c=c,y0=ones(1),lcon=lcon,ucon=Inf*ones(1))
# rewrite
9 nlp=ADNLPModel(f,fill(1.,2), lvar=fill(-Inf,2), uvar=fill(Inf,2), c=c,y0=fill(1.,1),lcon=lcon,ucon=fill(Inf,1))
# summary
ad hoc fill 3
ones, fill longer 2
# orig
trms = push!(Any[], remat(:(1|Batch), ds), ones(30, 1), reshape(y, (length(y), 1)))
8 @test_throws DomainError LinearMixedModel(f, mf, trms, Λ, fill(-1., 30))
17 modl = lmm(Reaction ~ 1 + Days + (1 + Days | Subject), slp)
18 @test_throws DimensionMismatch LinAlg.Ac_ldiv_B!(UpperTriangular(modl.R[1, 1]), ones(30, 1))
# rewrite
trms = push!(Any[], remat(:(1|Batch), ds), fill(1., 30, 1), reshape(y, (length(y), 1)))
8 @test_throws DomainError LinearMixedModel(f, mf, trms, Λ, fill(-1., 30))
17 modl = lmm(Reaction ~ 1 + Days + (1 + Days | Subject), slp)
18 @test_throws DimensionMismatch LinAlg.Ac_ldiv_B!(UpperTriangular(modl.R[1, 1]), Matrix{Float64}(30, 1))
# summary
ad hoc junk matrix constructor 1
ones, fill longer 1
# orig
16 @test neighborsum(ones(Int, 1)) == 0
17 @test neighborsum(ones(Int, 1, 1)) == 0
19 @test neighborsum(ones(Int, 1, 2)) == 2
# rewrite
16 @test neighborsum(fill(1, 1)) == 0
17 @test neighborsum(fill(1, 1, 1)) == 0
19 @test neighborsum(fill(1, 1, 2)) == 2
# summary
ones, fill shorter 3
# orig
blank_image = TensorFlow.constant(ones((5,5,5)))
6 result = run(sess, TensorFlow.image.flip_up_down(blank_image))
7 @test ones(5,5,5) == result
8
9 result = run(sess, TensorFlow.image.flip_left_right(blank_image))
10 @test ones(5,5,5) == result
# rewrite
blank_image = TensorFlow.constant(fill(1., (5,5,5)))
6 result = run(sess, TensorFlow.image.flip_up_down(blank_image))
7 @test fill(1.,5,5,5) == result
8
9 result = run(sess, TensorFlow.image.flip_left_right(blank_image))
10 @test fill(1.,5,5,5) == result
# summary
ones, fill longer 3
# orig
7 a=[0.02*ones(Ne); 0.02+0.08*ri];
8 b=[0.2*ones(Ne); 0.25-0.05*ri];
17 v=-65*ones(N)
19 c=[-65+15*re.^2; -65*ones(Ni)]
20 d=[8-6*re.^2; 2*ones(Ni)]
# rewrite
7 a=[fill(0.02,Ne); 0.02+0.08*ri];
8 b=[fill(0.2,Ne); 0.25-0.05*ri];
17 v=fill(-65,N)
19 c=[-65+15*re.^2; fill(-65,Ni)]
20 d=[8-6*re.^2; fill(2,Ni)]
# summary
ad hoc fill 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment