summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES.md3
-rw-r--r--examples/Real.hs19
-rw-r--r--examples/ode.hs16
-rw-r--r--lib/Numeric/GSL/ODE.hs47
-rw-r--r--lib/Numeric/GSL/gsl-aux.c62
-rw-r--r--lib/Numeric/LinearAlgebra/Algorithms.hs2
-rw-r--r--packages/tests/hmatrix-tests.cabal2
-rw-r--r--packages/tests/src/Numeric/LinearAlgebra/Tests.hs42
8 files changed, 145 insertions, 48 deletions
diff --git a/CHANGES.md b/CHANGES.md
index de466e5..10be500 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -3,6 +3,9 @@
3 3
4- integration over infinite intervals 4- integration over infinite intervals
5 5
6- msadams and msbdf methods for ode
7
8
60.13.0.0 90.13.0.0
7-------- 10--------
8 11
diff --git a/examples/Real.hs b/examples/Real.hs
index 02eba9f..9083b87 100644
--- a/examples/Real.hs
+++ b/examples/Real.hs
@@ -11,10 +11,10 @@ module Real(
11 row, 11 row,
12 col, 12 col,
13 (#),(&), (//), blocks, 13 (#),(&), (//), blocks,
14 rand 14 rand, randn
15) where 15) where
16 16
17import Numeric.LinearAlgebra hiding ((<>), (<|>), (<->), (<\>), (.*), (*/)) 17import Numeric.LinearAlgebra hiding ((<>), (<\>))
18import System.Random(randomIO) 18import System.Random(randomIO)
19 19
20infixl 7 <> 20infixl 7 <>
@@ -44,12 +44,21 @@ infixl 7 \>
44m \> v = flatten (m <\> reshape 1 v) 44m \> v = flatten (m <\> reshape 1 v)
45 45
46-- | Pseudorandom matrix with uniform elements between 0 and 1. 46-- | Pseudorandom matrix with uniform elements between 0 and 1.
47rand :: Int -- ^ rows 47randm :: RandDist
48 -> Int -- ^ rows
48 -> Int -- ^ columns 49 -> Int -- ^ columns
49 -> IO (Matrix Double) 50 -> IO (Matrix Double)
50rand r c = do 51randm d r c = do
51 seed <- randomIO 52 seed <- randomIO
52 return (reshape c $ randomVector seed Uniform (r*c)) 53 return (reshape c $ randomVector seed d (r*c))
54
55-- | Pseudorandom matrix with uniform elements between 0 and 1.
56rand :: Int -> Int -> IO (Matrix Double)
57rand = randm Uniform
58
59-- | Pseudorandom matrix with normal elements
60randn :: Int -> Int -> IO (Matrix Double)
61randn = randm Gaussian
53 62
54-- | Real identity matrix. 63-- | Real identity matrix.
55eye :: Int -> Matrix Double 64eye :: Int -> Matrix Double
diff --git a/examples/ode.hs b/examples/ode.hs
index 082c46c..dc6e0ec 100644
--- a/examples/ode.hs
+++ b/examples/ode.hs
@@ -1,6 +1,9 @@
1{-# LANGUAGE ViewPatterns #-}
1import Numeric.GSL.ODE 2import Numeric.GSL.ODE
2import Numeric.LinearAlgebra 3import Numeric.LinearAlgebra
3import Graphics.Plot 4import Graphics.Plot
5import Debug.Trace(trace)
6debug x = trace (show x) x
4 7
5vanderpol mu = do 8vanderpol mu = do
6 let xdot mu t [x,v] = [v, -x + mu * v * (1-x^2)] 9 let xdot mu t [x,v] = [v, -x + mu * v * (1-x^2)]
@@ -32,3 +35,16 @@ main = do
32 harmonic 1 0.1 35 harmonic 1 0.1
33 kepler 0.3 60 36 kepler 0.3 60
34 kepler 0.4 70 37 kepler 0.4 70
38 vanderpol' 2
39
40-- example of odeSolveV with jacobian
41vanderpol' mu = do
42 let xdot mu t (toList->[x,v]) = fromList [v, -x + mu * v * (1-x^2)]
43 jac t (toList->[x,v]) = (2><2) [ 0 , 1
44 , -1-2*x*v*mu, mu*(1-x**2) ]
45 ts = linspace 1000 (0,50)
46 hi = (ts@>1 - ts@>0)/100
47 sol = toColumns $ odeSolveV (MSBDF jac) hi 1E-8 1E-8 (xdot mu) (fromList [1,0]) ts
48 mplot sol
49
50
diff --git a/lib/Numeric/GSL/ODE.hs b/lib/Numeric/GSL/ODE.hs
index 2251acd..c243f4b 100644
--- a/lib/Numeric/GSL/ODE.hs
+++ b/lib/Numeric/GSL/ODE.hs
@@ -29,7 +29,7 @@ main = mplot (ts : toColumns sol)@
29----------------------------------------------------------------------------- 29-----------------------------------------------------------------------------
30 30
31module Numeric.GSL.ODE ( 31module Numeric.GSL.ODE (
32 odeSolve, odeSolveV, ODEMethod(..) 32 odeSolve, odeSolveV, ODEMethod(..), Jacobian
33) where 33) where
34 34
35import Data.Packed.Internal 35import Data.Packed.Internal
@@ -41,18 +41,21 @@ import System.IO.Unsafe(unsafePerformIO)
41 41
42------------------------------------------------------------------------- 42-------------------------------------------------------------------------
43 43
44type Jacobian = Double -> Vector Double -> Matrix Double
45
44-- | Stepping functions 46-- | Stepping functions
45data ODEMethod = RK2 -- ^ Embedded Runge-Kutta (2, 3) method. 47data ODEMethod = RK2 -- ^ Embedded Runge-Kutta (2, 3) method.
46 | RK4 -- ^ 4th order (classical) Runge-Kutta. The error estimate is obtained by halving the step-size. For more efficient estimate of the error, use 'RKf45'. 48 | RK4 -- ^ 4th order (classical) Runge-Kutta. The error estimate is obtained by halving the step-size. For more efficient estimate of the error, use the embedded methods.
47 | RKf45 -- ^ Embedded Runge-Kutta-Fehlberg (4, 5) method. This method is a good general-purpose integrator. 49 | RKf45 -- ^ Embedded Runge-Kutta-Fehlberg (4, 5) method. This method is a good general-purpose integrator.
48 | RKck -- ^ Embedded Runge-Kutta Cash-Karp (4, 5) method. 50 | RKck -- ^ Embedded Runge-Kutta Cash-Karp (4, 5) method.
49 | RK8pd -- ^ Embedded Runge-Kutta Prince-Dormand (8,9) method. 51 | RK8pd -- ^ Embedded Runge-Kutta Prince-Dormand (8,9) method.
50 | RK2imp -- ^ Implicit 2nd order Runge-Kutta at Gaussian points. 52 | RK2imp Jacobian -- ^ Implicit 2nd order Runge-Kutta at Gaussian points.
51 | RK4imp -- ^ Implicit 4th order Runge-Kutta at Gaussian points. 53 | RK4imp Jacobian -- ^ Implicit 4th order Runge-Kutta at Gaussian points.
52 | BSimp -- ^ Implicit Bulirsch-Stoer method of Bader and Deuflhard. This algorithm requires the Jacobian. 54 | BSimp Jacobian -- ^ Implicit Bulirsch-Stoer method of Bader and Deuflhard. The method is generally suitable for stiff problems.
53 | Gear1 -- ^ M=1 implicit Gear method. 55 | RK1imp Jacobian -- ^ Implicit Gaussian first order Runge-Kutta. Also known as implicit Euler or backward Euler method. Error estimation is carried out by the step doubling method.
54 | Gear2 -- ^ M=2 implicit Gear method. 56 | MSAdams -- ^ A variable-coefficient linear multistep Adams method in Nordsieck form. This stepper uses explicit Adams-Bashforth (predictor) and implicit Adams-Moulton (corrector) methods in P(EC)^m functional iteration mode. Method order varies dynamically between 1 and 12.
55 deriving (Enum,Eq,Show,Bounded) 57 | MSBDF Jacobian -- ^ A variable-coefficient linear multistep backward differentiation formula (BDF) method in Nordsieck form. This stepper uses the explicit BDF formula as predictor and implicit BDF formula as corrector. A modified Newton iteration method is used to solve the system of non-linear equations. Method order varies dynamically between 1 and 5. The method is generally suitable for stiff problems.
58
56 59
57-- | A version of 'odeSolveV' with reasonable default parameters and system of equations defined using lists. 60-- | A version of 'odeSolveV' with reasonable default parameters and system of equations defined using lists.
58odeSolve 61odeSolve
@@ -60,7 +63,7 @@ odeSolve
60 -> [Double] -- ^ initial conditions 63 -> [Double] -- ^ initial conditions
61 -> Vector Double -- ^ desired solution times 64 -> Vector Double -- ^ desired solution times
62 -> Matrix Double -- ^ solution 65 -> Matrix Double -- ^ solution
63odeSolve xdot xi ts = odeSolveV RKf45 hi epsAbs epsRel (l2v xdot) Nothing (fromList xi) ts 66odeSolve xdot xi ts = odeSolveV RKf45 hi epsAbs epsRel (l2v xdot) (fromList xi) ts
64 where hi = (ts@>1 - ts@>0)/100 67 where hi = (ts@>1 - ts@>0)/100
65 epsAbs = 1.49012e-08 68 epsAbs = 1.49012e-08
66 epsRel = 1.49012e-08 69 epsRel = 1.49012e-08
@@ -73,11 +76,33 @@ odeSolveV
73 -> Double -- ^ absolute tolerance for the state vector 76 -> Double -- ^ absolute tolerance for the state vector
74 -> Double -- ^ relative tolerance for the state vector 77 -> Double -- ^ relative tolerance for the state vector
75 -> (Double -> Vector Double -> Vector Double) -- ^ xdot(t,x) 78 -> (Double -> Vector Double -> Vector Double) -- ^ xdot(t,x)
79 -> Vector Double -- ^ initial conditions
80 -> Vector Double -- ^ desired solution times
81 -> Matrix Double -- ^ solution
82odeSolveV RK2 = odeSolveV' 0 Nothing
83odeSolveV RK4 = odeSolveV' 1 Nothing
84odeSolveV RKf45 = odeSolveV' 2 Nothing
85odeSolveV RKck = odeSolveV' 3 Nothing
86odeSolveV RK8pd = odeSolveV' 4 Nothing
87odeSolveV (RK2imp jac) = odeSolveV' 5 (Just jac)
88odeSolveV (RK4imp jac) = odeSolveV' 6 (Just jac)
89odeSolveV (BSimp jac) = odeSolveV' 7 (Just jac)
90odeSolveV (RK1imp jac) = odeSolveV' 8 (Just jac)
91odeSolveV MSAdams = odeSolveV' 9 Nothing
92odeSolveV (MSBDF jac) = odeSolveV' 10 (Just jac)
93
94
95odeSolveV'
96 :: CInt
76 -> Maybe (Double -> Vector Double -> Matrix Double) -- ^ optional jacobian 97 -> Maybe (Double -> Vector Double -> Matrix Double) -- ^ optional jacobian
98 -> Double -- ^ initial step size
99 -> Double -- ^ absolute tolerance for the state vector
100 -> Double -- ^ relative tolerance for the state vector
101 -> (Double -> Vector Double -> Vector Double) -- ^ xdot(t,x)
77 -> Vector Double -- ^ initial conditions 102 -> Vector Double -- ^ initial conditions
78 -> Vector Double -- ^ desired solution times 103 -> Vector Double -- ^ desired solution times
79 -> Matrix Double -- ^ solution 104 -> Matrix Double -- ^ solution
80odeSolveV method h epsAbs epsRel f mbjac xiv ts = unsafePerformIO $ do 105odeSolveV' method mbjac h epsAbs epsRel f xiv ts = unsafePerformIO $ do
81 let n = dim xiv 106 let n = dim xiv
82 fp <- mkDoubleVecVecfun (\t -> aux_vTov (checkdim1 n . f t)) 107 fp <- mkDoubleVecVecfun (\t -> aux_vTov (checkdim1 n . f t))
83 jp <- case mbjac of 108 jp <- case mbjac of
@@ -86,7 +111,7 @@ odeSolveV method h epsAbs epsRel f mbjac xiv ts = unsafePerformIO $ do
86 sol <- vec xiv $ \xiv' -> 111 sol <- vec xiv $ \xiv' ->
87 vec (checkTimes ts) $ \ts' -> 112 vec (checkTimes ts) $ \ts' ->
88 createMIO (dim ts) n 113 createMIO (dim ts) n
89 (ode_c (fi (fromEnum method)) h epsAbs epsRel fp jp // xiv' // ts' ) 114 (ode_c (method) h epsAbs epsRel fp jp // xiv' // ts' )
90 "ode" 115 "ode"
91 freeHaskellFunPtr fp 116 freeHaskellFunPtr fp
92 return sol 117 return sol
diff --git a/lib/Numeric/GSL/gsl-aux.c b/lib/Numeric/GSL/gsl-aux.c
index 5c17836..7f1cf68 100644
--- a/lib/Numeric/GSL/gsl-aux.c
+++ b/lib/Numeric/GSL/gsl-aux.c
@@ -32,7 +32,7 @@
32#include <gsl/gsl_complex_math.h> 32#include <gsl/gsl_complex_math.h>
33#include <gsl/gsl_rng.h> 33#include <gsl/gsl_rng.h>
34#include <gsl/gsl_randist.h> 34#include <gsl/gsl_randist.h>
35#include <gsl/gsl_odeiv.h> 35#include <gsl/gsl_odeiv2.h>
36#include <gsl/gsl_multifit_nlin.h> 36#include <gsl/gsl_multifit_nlin.h>
37#include <string.h> 37#include <string.h>
38#include <stdio.h> 38#include <stdio.h>
@@ -1356,38 +1356,38 @@ int ode(int method, double h, double eps_abs, double eps_rel,
1356 int jac(double, int, const double*, int, int, double*), 1356 int jac(double, int, const double*, int, int, double*),
1357 KRVEC(xi), KRVEC(ts), RMAT(sol)) { 1357 KRVEC(xi), KRVEC(ts), RMAT(sol)) {
1358 1358
1359 const gsl_odeiv_step_type * T; 1359 const gsl_odeiv2_step_type * T;
1360 1360
1361 switch(method) { 1361 switch(method) {
1362 case 0 : {T = gsl_odeiv_step_rk2; break; } 1362 case 0 : {T = gsl_odeiv2_step_rk2; break; }
1363 case 1 : {T = gsl_odeiv_step_rk4; break; } 1363 case 1 : {T = gsl_odeiv2_step_rk4; break; }
1364 case 2 : {T = gsl_odeiv_step_rkf45; break; } 1364 case 2 : {T = gsl_odeiv2_step_rkf45; break; }
1365 case 3 : {T = gsl_odeiv_step_rkck; break; } 1365 case 3 : {T = gsl_odeiv2_step_rkck; break; }
1366 case 4 : {T = gsl_odeiv_step_rk8pd; break; } 1366 case 4 : {T = gsl_odeiv2_step_rk8pd; break; }
1367 case 5 : {T = gsl_odeiv_step_rk2imp; break; } 1367 case 5 : {T = gsl_odeiv2_step_rk2imp; break; }
1368 case 6 : {T = gsl_odeiv_step_rk4imp; break; } 1368 case 6 : {T = gsl_odeiv2_step_rk4imp; break; }
1369 case 7 : {T = gsl_odeiv_step_bsimp; break; } 1369 case 7 : {T = gsl_odeiv2_step_bsimp; break; }
1370 case 8 : {T = gsl_odeiv_step_gear1; break; } 1370 case 8 : {T = gsl_odeiv2_step_rk1imp; break; }
1371 case 9 : {T = gsl_odeiv_step_gear2; break; } 1371 case 9 : {T = gsl_odeiv2_step_msadams; break; }
1372 case 10: {T = gsl_odeiv2_step_msbdf; break; }
1372 default: ERROR(BAD_CODE); 1373 default: ERROR(BAD_CODE);
1373 } 1374 }
1374 1375
1375
1376 gsl_odeiv_step * s = gsl_odeiv_step_alloc (T, xin);
1377 gsl_odeiv_control * c = gsl_odeiv_control_y_new (eps_abs, eps_rel);
1378 gsl_odeiv_evolve * e = gsl_odeiv_evolve_alloc (xin);
1379
1380 Tode P; 1376 Tode P;
1381 P.f = f; 1377 P.f = f;
1382 P.j = jac; 1378 P.j = jac;
1383 P.n = xin; 1379 P.n = xin;
1384 1380
1385 gsl_odeiv_system sys = {odefunc, odejac, xin, &P}; 1381 gsl_odeiv2_system sys = {odefunc, odejac, xin, &P};
1382
1383 gsl_odeiv2_driver * d =
1384 gsl_odeiv2_driver_alloc_y_new (&sys, T, h, eps_abs, eps_rel);
1386 1385
1387 double t = tsp[0]; 1386 double t = tsp[0];
1388 1387
1389 double* y = (double*)calloc(xin,sizeof(double)); 1388 double* y = (double*)calloc(xin,sizeof(double));
1390 int i,j; 1389 int i,j;
1390 int status;
1391 for(i=0; i< xin; i++) { 1391 for(i=0; i< xin; i++) {
1392 y[i] = xip[i]; 1392 y[i] = xip[i];
1393 solp[i] = xip[i]; 1393 solp[i] = xip[i];
@@ -1396,22 +1396,24 @@ int ode(int method, double h, double eps_abs, double eps_rel,
1396 for (i = 1; i < tsn ; i++) 1396 for (i = 1; i < tsn ; i++)
1397 { 1397 {
1398 double ti = tsp[i]; 1398 double ti = tsp[i];
1399 while (t < ti) 1399
1400 { 1400 status = gsl_odeiv2_driver_apply (d, &t, ti, y);
1401 gsl_odeiv_evolve_apply (e, c, s, 1401
1402 &sys, 1402 if (status != GSL_SUCCESS) {
1403 &t, ti, &h, 1403 printf ("error in ode, return value=%d\n", status);
1404 y); 1404 break;
1405 // if (h < hmin) h = hmin; 1405 }
1406 } 1406
1407// printf ("%.5e %.5e %.5e\n", t, y[0], y[1]);
1408
1407 for(j=0; j<xin; j++) { 1409 for(j=0; j<xin; j++) {
1408 solp[i*xin + j] = y[j]; 1410 solp[i*xin + j] = y[j];
1409 } 1411 }
1410 } 1412 }
1411 1413
1412 free(y); 1414 free(y);
1413 gsl_odeiv_evolve_free (e); 1415 gsl_odeiv2_driver_free (d);
1414 gsl_odeiv_control_free (c); 1416
1415 gsl_odeiv_step_free (s); 1417 return status;
1416 return 0;
1417} 1418}
1419
diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs
index e2ecd4d..435cc5a 100644
--- a/lib/Numeric/LinearAlgebra/Algorithms.hs
+++ b/lib/Numeric/LinearAlgebra/Algorithms.hs
@@ -209,7 +209,7 @@ rightSV :: Field t => Matrix t -> (Vector Double, Matrix t)
209rightSV m | vertical m = let (_,s,v) = thinSVD m in (s,v) 209rightSV m | vertical m = let (_,s,v) = thinSVD m in (s,v)
210 | otherwise = let (_,s,v) = svd m in (s,v) 210 | otherwise = let (_,s,v) = svd m in (s,v)
211 211
212-- | Singular values and all right singular vectors. 212-- | Singular values and all left singular vectors.
213leftSV :: Field t => Matrix t -> (Matrix t, Vector Double) 213leftSV :: Field t => Matrix t -> (Matrix t, Vector Double)
214leftSV m | vertical m = let (u,s,_) = svd m in (u,s) 214leftSV m | vertical m = let (u,s,_) = svd m in (u,s)
215 | otherwise = let (u,s,_) = thinSVD m in (u,s) 215 | otherwise = let (u,s,_) = thinSVD m in (u,s)
diff --git a/packages/tests/hmatrix-tests.cabal b/packages/tests/hmatrix-tests.cabal
index 9f7bcdc..d1f449b 100644
--- a/packages/tests/hmatrix-tests.cabal
+++ b/packages/tests/hmatrix-tests.cabal
@@ -1,5 +1,5 @@
1Name: hmatrix-tests 1Name: hmatrix-tests
2Version: 0.1.0.0 2Version: 0.1.1
3License: GPL 3License: GPL
4License-file: LICENSE 4License-file: LICENSE
5Author: Alberto Ruiz 5Author: Alberto Ruiz
diff --git a/packages/tests/src/Numeric/LinearAlgebra/Tests.hs b/packages/tests/src/Numeric/LinearAlgebra/Tests.hs
index 8d402d0..7c41209 100644
--- a/packages/tests/src/Numeric/LinearAlgebra/Tests.hs
+++ b/packages/tests/src/Numeric/LinearAlgebra/Tests.hs
@@ -43,6 +43,8 @@ import Control.Arrow((***))
43import Debug.Trace 43import Debug.Trace
44import Control.Monad(when) 44import Control.Monad(when)
45 45
46import Data.Packed.ST
47
46import Test.QuickCheck(Arbitrary,arbitrary,coarbitrary,choose,vector 48import Test.QuickCheck(Arbitrary,arbitrary,coarbitrary,choose,vector
47 ,sized,classify,Testable,Property 49 ,sized,classify,Testable,Property
48 ,quickCheckWithResult,maxSize,stdArgs,shrink) 50 ,quickCheckWithResult,maxSize,stdArgs,shrink)
@@ -622,6 +624,7 @@ runBenchmarks :: IO ()
622runBenchmarks = do 624runBenchmarks = do
623 solveBench 625 solveBench
624 subBench 626 subBench
627 mkVecBench
625 multBench 628 multBench
626 cholBench 629 cholBench
627 svdBench 630 svdBench
@@ -638,6 +641,14 @@ time msg act = do
638 printf "%6.2f s CPU\n" $ (fromIntegral (t1 - t0) / (10^12 :: Double)) :: IO () 641 printf "%6.2f s CPU\n" $ (fromIntegral (t1 - t0) / (10^12 :: Double)) :: IO ()
639 return () 642 return ()
640 643
644timeR msg act = do
645 putStr (msg++" ")
646 t0 <- getCPUTime
647 putStr (show act)
648 t1 <- getCPUTime
649 printf "%6.2f s CPU\n" $ (fromIntegral (t1 - t0) / (10^12 :: Double)) :: IO ()
650 return ()
651
641-------------------------------- 652--------------------------------
642 653
643manymult n = foldl1' (<>) (map rot2 angles) where 654manymult n = foldl1' (<>) (map rot2 angles) where
@@ -653,6 +664,37 @@ multb n = foldl1' (<>) (replicate (10^6) (ident n :: Matrix Double))
653 664
654-------------------------------- 665--------------------------------
655 666
667manyvec0 xs = sum $ map (\x -> x + x**2 + x**3) xs
668manyvec1 xs = sumElements $ fromRows $ map (\x -> fromList [x,x**2,x**3]) xs
669manyvec5 xs = sumElements $ fromRows $ map (\x -> vec3 x (x**2) (x**3)) xs
670
671
672manyvec2 xs = sum $ map (\x -> sqrt(x^2 + (x**2)^2 +(x**3)^2)) xs
673manyvec3 xs = sum $ map (pnorm PNorm2 . (\x -> fromList [x,x**2,x**3])) xs
674
675manyvec4 xs = sum $ map (pnorm PNorm2 . (\x -> vec3 x (x**2) (x**3))) xs
676
677vec3 :: Double -> Double -> Double -> Vector Double
678vec3 a b c = runSTVector $ do
679 v <- newUndefinedVector 3
680 writeVector v 0 a
681 writeVector v 1 b
682 writeVector v 2 c
683 return v
684
685mkVecBench = do
686 let n = 1000000
687 xs = toList $ linspace n (0,1::Double)
688 putStr "\neval data... "; print (sum xs)
689 timeR "listproc " $ manyvec0 xs
690 timeR "fromList matrix " $ manyvec1 xs
691 timeR "vec3 matrix " $ manyvec5 xs
692 timeR "listproc norm " $ manyvec2 xs
693 timeR "norm fromList " $ manyvec3 xs
694 timeR "norm vec3 " $ manyvec4 xs
695
696--------------------------------
697
656subBench = do 698subBench = do
657 putStrLn "" 699 putStrLn ""
658 let g = foldl1' (.) (replicate (10^5) (\v -> subVector 1 (dim v -1) v)) 700 let g = foldl1' (.) (replicate (10^5) (\v -> subVector 1 (dim v -1) v))