Re: Special Matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg28327] Re: Special Matrix
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Wed, 11 Apr 2001 02:01:06 -0400 (EDT)
- References: <9ah5nq$ps0@smc.vnet.net> <9ajmap$s9e@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
There is a problem with the code: CreateMatrix[n_]:=Block[{r,c,m},Needs["LinearAlgebra`MatrixManipulation`"]; r=ZeroMatrix[1,n];m=Table[1,{n-2},{n-2}]; c=ZeroMatrix[n-2,1]; BlockMatrix[{{r},{c,m,c},{r}}]] As shown below CreateMatrix[5] BlockMatrix::shdw: Symbol BlockMatrix appears in multiple contexts \ {LinearAlgebra`MatrixManipulation`,Global`}; definitions in context \ LinearAlgebra`MatrixManipulation` may shadow or be shadowed by other \ definitions. ZeroMatrix::shdw: Symbol ZeroMatrix appears in multiple contexts \ {LinearAlgebra`MatrixManipulation`,Global`}; definitions in context \ LinearAlgebra`MatrixManipulation` may shadow or be shadowed by other \ definitions. BlockMatrix[{{ZeroMatrix[1,5]},{ZeroMatrix[3,1],{{1,1,1},{1,1,1},{1,1,1}}, ZeroMatrix[3,1]},{ZeroMatrix[1,5]}}] This is because the context of the symbols ZeroMatrix and BlockMatrix are prepended when the definion is enterered : they become Global`ZeroMatrix Global`BlockMatrix When CreateMatrix[5] is evaluated the package LinearAlgebra`MatrixManipulation` is loaded and creates the symbols LinearAlgebra`MatrixManipulation`ZeroMatrix LinearAlgebra`MatrixManipulation`BlockMatrix with their definitions. Unfortunately Global`ZeroMatrix , Global`BlockMatrix do not pick up these definitions. Here are two ways of avoiding this problem 1) Load the package before loading defining CreateMatrix Needs["LinearAlgebra`MatrixManipulation`"]; CreateMatrix[n_]:= Block[{r,c,m}, r=ZeroMatrix[1,n];m=Table[1,{n-2},{n-2}]; c=ZeroMatrix[n-2,1]; BlockMatrix[{{r},{c,m,c},{r}}]] 2) Give ZeroMatrix and BlockMatrix the long names that loading the package will make. Needs["LinearAlgebra`MatrixManipulation`"]; CreateMatrix[n_]:= Block[{r,c,m}, r= LinearAlgebra`MatrixManipulation`ZeroMatrix[1,n]; m=Table[1,{n-2},{n-2}]; c=LinearAlgebra`MatrixManipulation`ZeroMatrix[n-2,1]; LinearAlgebra`MatrixManipulation`BlockMatrix[{{r},{c,m,c},{r}}]] -- Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 "Souvik Banerjee" <s-banerjee at nwu.edu> wrote in message news:9ajmap$s9e at smc.vnet.net... > Not sure if the shortest but it works: > > CreateMatrix[n_] := > Block[{r,c,m}, > Needs["LinearAlgebra`MatrixManipulation`"]; > r = ZeroMatrix[1, n]; m = Table[1, {n - 2}, {n - 2}]; > c = ZeroMatrix[n - 2, 1]; > BlockMatrix[{{r}, {c, m, c}, {r}}] > ] > > The bottleneck seems to be the Table[] operations. If there is a built in > function to create a matrix with a constant element then it will speed this > up. > > -Souvik > > > Yoram Pollack <syftech at saad.org.il> wrote in message > news:9ah5nq$ps0 at smc.vnet.net... > > Hello > > I am trying to create a "n x n" matrix with "1" in every position exept in > > first and last row, and first and last coulumn, where "0" is needed. In > > other words: sqare matrix full of "1" sorounded by "0". > > What will be the fastest and shortest way to do it? > > > > Thanks > > Yoram > > > > > > > > >