In [ ]:

In [1]:
# nteract - one way I like this
In [5]:
function solve_linsys(A::Matrix, b::Vector)
  x = A\b # this is calling Julia's built in "Gaussian Elimination with pivoting" 
end
Out[5]:
solve_linsys (generic function with 1 method)
In [6]:
solve_linsys(rand(5,5),rand(5))
Out[6]:
5-element Array{Float64,1}:
 -1.5891390396338654
 -1.513857014471811
  1.5275420895708063
  0.3232581954448325
  1.799604390064287
In [7]:
function mult_alg(x::Vector)
  p = x[1]
  for i=2:length(x)
    p = p*x[i]
  end
  return p
end
x = 1:25
mult_alg(x)
MethodError: no method matching mult_alg(::UnitRange{Int64})
Closest candidates are:
  mult_alg(!Matched::Array{T,1} where T) at In[7]:1

Stacktrace:
 [1] top-level scope at In[7]:9
 [2] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091
In [12]:
xx = 1:25
2 .*(xx .+ 5)
Out[12]:
12:2:60
In [14]:
collect(2 .*(xx .+ 5))
Out[14]:
25-element Array{Int64,1}:
 12
 14
 16
 18
 20
 22
 24
 26
 28
 30
 32
 34
 36
 38
 40
 42
 44
 46
 48
 50
 52
 54
 56
 58
 60
In [16]:
function mult_alg(x::AbstractVector)
  p = x[1]
  for i=2:length(x)
    p = p*x[i]
  end
  return p
end
x = 1:10
mult_alg(x)
Out[16]:
3628800
In [ ]:
# collect ranges into vectors... to get specific data...
# watch out for types!
In [17]:
mult_alg(1:25)
Out[17]:
7034535277573963776
In [18]:
mult_alg(1.0:25.0)
Out[18]:
1.5511210043330986e25
In [19]:
# Let's see what happens..
for i=1:25
  @show mult_alg(1:i)
  @show mult_alg(1:Float64(i))
end
mult_alg(1:i) = 1
mult_alg(1:Float64(i)) = 1.0
mult_alg(1:i) = 2
mult_alg(1:Float64(i)) = 2.0
mult_alg(1:i) = 6
mult_alg(1:Float64(i)) = 6.0
mult_alg(1:i) = 24
mult_alg(1:Float64(i)) = 24.0
mult_alg(1:i) = 120
mult_alg(1:Float64(i)) = 120.0
mult_alg(1:i) = 720
mult_alg(1:Float64(i)) = 720.0
mult_alg(1:i) = 5040
mult_alg(1:Float64(i)) = 5040.0
mult_alg(1:i) = 40320
mult_alg(1:Float64(i)) = 40320.0
mult_alg(1:i) = 362880
mult_alg(1:Float64(i)) = 362880.0
mult_alg(1:i) = 3628800
mult_alg(1:Float64(i)) = 3.6288e6
mult_alg(1:i) = 39916800
mult_alg(1:Float64(i)) = 3.99168e7
mult_alg(1:i) = 479001600
mult_alg(1:Float64(i)) = 4.790016e8
mult_alg(1:i) = 6227020800
mult_alg(1:Float64(i)) = 6.2270208e9
mult_alg(1:i) = 87178291200
mult_alg(1:Float64(i)) = 8.71782912e10
mult_alg(1:i) = 1307674368000
mult_alg(1:Float64(i)) = 1.307674368e12
mult_alg(1:i) = 20922789888000
mult_alg(1:Float64(i)) = 2.0922789888e13
mult_alg(1:i) = 355687428096000
mult_alg(1:Float64(i)) = 3.55687428096e14
mult_alg(1:i) = 6402373705728000
mult_alg(1:Float64(i)) = 6.402373705728e15
mult_alg(1:i) = 121645100408832000
mult_alg(1:Float64(i)) = 1.21645100408832e17
mult_alg(1:i) = 2432902008176640000
mult_alg(1:Float64(i)) = 2.43290200817664e18
mult_alg(1:i) = -4249290049419214848
mult_alg(1:Float64(i)) = 5.109094217170944e19
mult_alg(1:i) = -1250660718674968576
mult_alg(1:Float64(i)) = 1.1240007277776077e21
mult_alg(1:i) = 8128291617894825984
mult_alg(1:Float64(i)) = 2.585201673888498e22
mult_alg(1:i) = -7835185981329244160
mult_alg(1:Float64(i)) = 6.204484017332394e23
mult_alg(1:i) = 7034535277573963776
mult_alg(1:Float64(i)) = 1.5511210043330986e25
In [20]:
0x01 # this is one!
Out[20]:
0x01
In [26]:
UInt256(1)
UndefVarError: UInt256 not defined

Stacktrace:
 [1] top-level scope at In[26]:1
 [2] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091
In [28]:
using LinearAlgebra
function bad_growth(n)
    A = Matrix(1.0I,n,n)
    A = A .- tril(ones(n,n),-1)
    A[:,end] .= 1
    return A
end
##
using Printf
function example(n)
    xtrue = (2*(rand(n,1) .< 0.5).-1)./10
    A = bad_growth(n)
    b = A*xtrue
    xcomp = A\b
    @printf("\n  n = %i\n\n", n);
    @printf("    [ %25s %25s ]\n", "x true", "x computed");
    for i=1:n
        @printf("    [ %25.17f %25.17f ]\n", xtrue[i], xcomp[i]);
    end
    @printf("norm(A*x - b) = %.18e", norm(A*xcomp - b))
end
example(10)
  n = 10

    [                    x true                x computed ]
    [       0.10000000000000001       0.10000000000000001 ]
    [      -0.10000000000000001      -0.10000000000000003 ]
    [      -0.10000000000000001      -0.09999999999999998 ]
    [      -0.10000000000000001      -0.09999999999999998 ]
    [      -0.10000000000000001      -0.10000000000000009 ]
    [       0.10000000000000001       0.10000000000000009 ]
    [       0.10000000000000001       0.09999999999999964 ]
    [       0.10000000000000001       0.09999999999999964 ]
    [      -0.10000000000000001      -0.10000000000000142 ]
    [      -0.10000000000000001      -0.10000000000000001 ]
norm(A*x - b) = 2.240478477256787034e-15
In [29]:
bad_growth(10)
Out[29]:
10×10 Array{Float64,2}:
  1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  1.0
 -1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  1.0
 -1.0  -1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0  1.0
 -1.0  -1.0  -1.0   1.0   0.0   0.0   0.0   0.0   0.0  1.0
 -1.0  -1.0  -1.0  -1.0   1.0   0.0   0.0   0.0   0.0  1.0
 -1.0  -1.0  -1.0  -1.0  -1.0   1.0   0.0   0.0   0.0  1.0
 -1.0  -1.0  -1.0  -1.0  -1.0  -1.0   1.0   0.0   0.0  1.0
 -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0   1.0   0.0  1.0
 -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0   1.0  1.0
 -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  1.0
In [30]:
example(60)
  n = 60

    [                    x true                x computed ]
    [       0.10000000000000001       0.10000000000000001 ]
    [       0.10000000000000001       0.10000000000000003 ]
    [      -0.10000000000000001      -0.09999999999999998 ]
    [      -0.10000000000000001      -0.09999999999999998 ]
    [      -0.10000000000000001      -0.10000000000000009 ]
    [       0.10000000000000001       0.10000000000000009 ]
    [       0.10000000000000001       0.09999999999999964 ]
    [      -0.10000000000000001      -0.09999999999999964 ]
    [       0.10000000000000001       0.10000000000000142 ]
    [      -0.10000000000000001      -0.09999999999999432 ]
    [       0.10000000000000001       0.10000000000000853 ]
    [       0.10000000000000001       0.10000000000002274 ]
    [       0.10000000000000001       0.10000000000002274 ]
    [       0.10000000000000001       0.10000000000013642 ]
    [       0.10000000000000001       0.10000000000013642 ]
    [      -0.10000000000000001      -0.09999999999945430 ]
    [       0.10000000000000001       0.10000000000036380 ]
    [       0.10000000000000001       0.10000000000218279 ]
    [       0.10000000000000001       0.10000000000218279 ]
    [      -0.10000000000000001      -0.09999999999126885 ]
    [       0.10000000000000001       0.10000000000582077 ]
    [      -0.10000000000000001      -0.09999999997671694 ]
    [       0.10000000000000001       0.10000000003492460 ]
    [      -0.10000000000000001      -0.09999999986030161 ]
    [      -0.10000000000000001      -0.09999999986030161 ]
    [       0.10000000000000001       0.10000000055879354 ]
    [       0.10000000000000001       0.10000000055879354 ]
    [      -0.10000000000000001      -0.09999999776482582 ]
    [       0.10000000000000001       0.10000000149011612 ]
    [       0.10000000000000001       0.10000000894069672 ]
    [       0.10000000000000001       0.10000000894069672 ]
    [      -0.10000000000000001      -0.09999996423721313 ]
    [      -0.10000000000000001      -0.09999996423721313 ]
    [      -0.10000000000000001      -0.09999990463256836 ]
    [      -0.10000000000000001      -0.09999990463256836 ]
    [       0.10000000000000001       0.10000038146972656 ]
    [       0.10000000000000001       0.10000038146972656 ]
    [       0.10000000000000001       0.10000228881835938 ]
    [      -0.10000000000000001      -0.09999847412109375 ]
    [       0.10000000000000001       0.10000610351562500 ]
    [      -0.10000000000000001      -0.09999084472656250 ]
    [       0.10000000000000001       0.10003662109375000 ]
    [      -0.10000000000000001      -0.09997558593750000 ]
    [       0.10000000000000001       0.10009765625000000 ]
    [      -0.10000000000000001      -0.09985351562500000 ]
    [       0.10000000000000001       0.10058593750000000 ]
    [       0.10000000000000001       0.10058593750000000 ]
    [       0.10000000000000001       0.10156250000000000 ]
    [      -0.10000000000000001      -0.09765625000000000 ]
    [      -0.10000000000000001      -0.09375000000000000 ]
    [      -0.10000000000000001      -0.09375000000000000 ]
    [       0.10000000000000001       0.12500000000000000 ]
    [       0.10000000000000001       0.12500000000000000 ]
    [      -0.10000000000000001       0.00000000000000000 ]
    [      -0.10000000000000001       0.00000000000000000 ]
    [      -0.10000000000000001       0.00000000000000000 ]
    [       0.10000000000000001       0.00000000000000000 ]
    [       0.10000000000000001       0.00000000000000000 ]
    [      -0.10000000000000001       0.00000000000000000 ]
    [       0.10000000000000001       0.10000000000000001 ]
norm(A*x - b) = 6.818385329201765988e-01
In [34]:
using Printf
function example2(n)
    xtrue = (2*(rand(n,1) .< 0.5).-1)./10
    A = bad_growth(n).+eps(1.0)*randn(n,n)
    b = A*xtrue
    xcomp = A\b
    @printf("\n  n = %i\n\n", n);
    @printf("    [ %25s %25s ]\n", "x true", "x computed");
    for i=1:n
        @printf("    [ %25.17f %25.17f ]\n", xtrue[i], xcomp[i]);
    end
    @printf("norm(A*x - b) = %.18e", norm(A*xcomp - b))
end
example2(60)
  n = 60

    [                    x true                x computed ]
    [      -0.10000000000000001      -0.09999999999999985 ]
    [       0.10000000000000001       0.09999999999999985 ]
    [       0.10000000000000001       0.10000000000000002 ]
    [       0.10000000000000001       0.10000000000000001 ]
    [       0.10000000000000001       0.09999999999999992 ]
    [       0.10000000000000001       0.10000000000000001 ]
    [       0.10000000000000001       0.10000000000000001 ]
    [       0.10000000000000001       0.10000000000000001 ]
    [      -0.10000000000000001      -0.09999999999999988 ]
    [      -0.10000000000000001      -0.10000000000000002 ]
    [      -0.10000000000000001      -0.09999999999999995 ]
    [      -0.10000000000000001      -0.09999999999999998 ]
    [       0.10000000000000001       0.09999999999999998 ]
    [       0.10000000000000001       0.10000000000000003 ]
    [       0.10000000000000001       0.09999999999999999 ]
    [       0.10000000000000001       0.09999999999999996 ]
    [      -0.10000000000000001      -0.09999999999999995 ]
    [      -0.10000000000000001      -0.10000000000000001 ]
    [      -0.10000000000000001      -0.09999999999999995 ]
    [       0.10000000000000001       0.10000000000000003 ]
    [      -0.10000000000000001      -0.10000000000000006 ]
    [       0.10000000000000001       0.10000000000000005 ]
    [       0.10000000000000001       0.09999999999999992 ]
    [      -0.10000000000000001      -0.10000000000000016 ]
    [      -0.10000000000000001      -0.09999999999999981 ]
    [       0.10000000000000001       0.09999999999999999 ]
    [      -0.10000000000000001      -0.09999999999999996 ]
    [      -0.10000000000000001      -0.09999999999999998 ]
    [       0.10000000000000001       0.09999999999999995 ]
    [      -0.10000000000000001      -0.10000000000000003 ]
    [      -0.10000000000000001      -0.09999999999999991 ]
    [      -0.10000000000000001      -0.09999999999999995 ]
    [       0.10000000000000001       0.10000000000000007 ]
    [      -0.10000000000000001      -0.10000000000000001 ]
    [      -0.10000000000000001      -0.10000000000000002 ]
    [       0.10000000000000001       0.10000000000000003 ]
    [      -0.10000000000000001      -0.09999999999999994 ]
    [      -0.10000000000000001      -0.10000000000000006 ]
    [      -0.10000000000000001      -0.10000000000000010 ]
    [      -0.10000000000000001      -0.09999999999999991 ]
    [      -0.10000000000000001      -0.10000000000000006 ]
    [      -0.10000000000000001      -0.10000000000000017 ]
    [       0.10000000000000001       0.10000000000000003 ]
    [      -0.10000000000000001      -0.09999999999999988 ]
    [       0.10000000000000001       0.09999999999999981 ]
    [       0.10000000000000001       0.09999999999999989 ]
    [      -0.10000000000000001      -0.09999999999999994 ]
    [       0.10000000000000001       0.10000000000000010 ]
    [      -0.10000000000000001      -0.10000000000000001 ]
    [       0.10000000000000001       0.10000000000000014 ]
    [      -0.10000000000000001      -0.10000000000000013 ]
    [       0.10000000000000001       0.10000000000000028 ]
    [      -0.10000000000000001      -0.10000000000000012 ]
    [       0.10000000000000001       0.09999999999999987 ]
    [      -0.10000000000000001      -0.10000000000000012 ]
    [       0.10000000000000001       0.10000000000000016 ]
    [      -0.10000000000000001      -0.10000000000000006 ]
    [      -0.10000000000000001      -0.09999999999999989 ]
    [      -0.10000000000000001      -0.09999999999999992 ]
    [       0.10000000000000001       0.10000000000000001 ]
norm(A*x - b) = 1.872473087592172155e-15