MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Show[list] does not work - details of the script

  • To: mathgroup at smc.vnet.net
  • Subject: [mg98410] Re: Show[list] does not work - details of the script
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Thu, 9 Apr 2009 05:54:04 -0400 (EDT)
  • References: <grhp7q$mi2$1@smc.vnet.net>

Frankly, this code is a bit of a mess. I'll go through it line by
line. Unfortunately, since it needs external data I cannot verify the
final result.

>Clear["Global` *"]
>ClearSystemCache["Numeric"];
>Clear[l];

Lots of Clears here. Usually, this is not necessary unless you're
afraid that you have made multiple (overloaded) definitions with the
same name. I'd suggest you remove all Clears from the program. There
are even Clears in the loop!

>plot1 = {};
>plot2 = {};
>myPlots = {};

Many initializations as well. In programming courses it is often
advised to initialize all the variables you use (if the programming
language you use not already does this by default).

In my opinion, in Mathematica it is better not to initialize variables
if there isn't a good reason to do that. If you make the error of
assuming your variable has a value when, in fact, it has not, your
variable will end up in the end result as a symbol without value, or
it will cause an error in between which will be a signal of a
programming logic error. In both ways, it is much clearer to see what
caused the error than when you had initialized the variable.

Of course, if you write everything in one large notebook you run the
risk of variables of one part being used  inadvertently in another.
It's better to work with local variables using constructions like
Module or Block.

In your code, you sometimes set a variable to a specific value just
after initialization. Happens with Data. Pretty senseless.

> $RecursionLimit = 10000;

This is a very high value. Usually, recursion quickly leads to a
combinatoric explosion. The number of calculations is often
proportinal to a constant to the power $RecursionLimit.

> as = {};
> lst = {};
> hg = 0;
> Clear[k];
> Data = {};

See above. hg is used in an Equals test once. Is its definition as a
variable necessary?

> dataFilenames1 = Flatten[Table[FileNames["*.new"], {1}]];

The Flatten and Table are superfluous, as FileNames already generates
a flat list.

> Data = Table[
>    ToString[
>     StringJoin["/home/venki/ten1/BMRB_files/",
>      ToString[dataFilenames1[[vh]]]]], {vh, 1, 100}];
>

You use ToString on things that are already strings. The path is a
string and the names in dataFilenames1 are also strings.

If the goal of this code is to get the whole path of the file attached
to its name you could have written

Data = FileNames["*.new","/home/venki/ten1/BMRB_files/"]

> Do[
>  ToString[Data[[m]]]

Again, superfluous use of ToString and a missing '; '

>   Clear[mat];
>  Clear[k];
>  Clear[l];
>  Clear[x];
>  Clear[z];

Unnecessary, remove.

>  k = Import[ToString[Data[[m]]], "Table"];

Again, superfluous use of ToString.

>  Grid[k];

>  MatrixForm[mat];

Two lines that do nothing because their output is suppressed by ;

>  mat = k;

>  l = Transpose[mat];
>  x = l[[1, All]];
>  z = l[[2, All]];

Calculation of x and z could be written somewhat shorter as:

x=mat[[1]];
z=mat[[2]];

Actually, I don't see why you introduce 'mat' here. It is identical to
k and is used in the assignment to l only. Could have used k there as
well.

>  Clear[kj];
>   lst = {};
>  For[i = 1, i <= 220, i = i + 1,
>   If[x[[i]] == hg, lst = Append[{z[[i]]}, lst]]];

lst = Append[{z[[i]]}, lst] could be written just as easily as lst = {z
[[i]], lst}. Your original line could be written better as lst = Append
[lst,z[[i]]] since you seem to want z[[i]] in the List lst and not the
other way around. Using lst = {lst,z[[i]]} is often much faster.

>  kj = Flatten[lst];
>  x1 = kj[[1]];
>  y1 = kj[[2]];
>  y2 = kj[[3]];

I have the impression something goes wrong here. lst is a list with
possibly up to 220 members, but it is not guranteed to be longer than
3. That depends upon the values in the vector x. Hence, the code here
may generate errors. Not sure if the first three are special here.

>  If[MemberQ[kj, x1] == True,

This is pretty silly. Tou assigned x1 to the first member of list kj.
If you then test whether x1 is in kj this must of course be true.

The '==True' part of of the test is not necessary. If the member test
yields True you end up with True==True, which is True. If it yields
False, you get False==True which equals False. In both cases, that's
just the result of the member test itself.

>     If[ MemberQ[kj, y2] == False,

You picked y2 from kj so the member test will yield true. The result
will be True == False, which will always be False.

>      plot1 = Flatten[Append[{ListPlot[{x1, y1}]}, plot1]]

Assuming that you want a scatter plot of x1 vs y1, and assuming x1 and
y1 are one-dimensional lists you want ListPlot[Transpose[{x1,y1}]]
since ListPlot needs a one dimensional array of values or of pairs of
values. But perhaps you want two plots of x1 and y1, then it's OK.

As above, you may avoid Append: plot1 = Flatten[{ListPlot[{x1, y1}],
plot1]}]

>      , plot2 =
>        Flatten[Append[{ListPlot[{{x1, y1}, {x1, y2}}]}, plot2]];

Still looks like you want a scatter plot of y1 or y2 against x.
ListPlot should then be used as:

ListPlot[{Transpose[{x1, y1}], Transpose[{x1, y2}]}]

>]]

Missing ';'

>    myPlots = Join[plot1, plot2];

You don't use myPlots in the remainder of the code...


> Part::partw: Part All of {} does not exist. >>
> Part::partw: Part All of {} does not exist. >>

It looks like the matrix isn't two-dimensional. Did you check whether
the Import succeed in getting k the correct matrix?

Cheers -- Sjoerd

On Apr 8, 11:02 am, Venkatesh Ramakrishnan <venk... at tifr.res.in>
wrote:
> Dear All
>
> i am kind of newbie in Mathematica programming,
> There are some errors which I am not able to troubleshoot...
>
> I would be very thankful, if someone can look into it....
>
> I have pasted the whole script and the errors-
> Clear["Global` *"]
> ClearSystemCache["Numeric"];
> Clear[l];
> plot1 = {};
> plot2 = {};
> myPlots = {};
> $RecursionLimit = 10000;
> as = {};
> lst = {};
> hg = 0;
> Clear[k];
> Data = {};
> dataFilenames1 = Flatten[Table[FileNames["*.new"], {1}]];
> Data = Table[
>    ToString[
>     StringJoin["/home/venki/ten1/BMRB_files/",
>      ToString[dataFilenames1[[vh]]]]], {vh, 1, 100}];
>
> Do[
>  ToString[Data[[m]]]
>   Clear[mat];
>  Clear[k];
>  Clear[l];
>  Clear[x];
>  Clear[z];
>  k = Import[ToString[Data[[m]]], "Table"];
>  Grid[k];
>  mat = k;
>  MatrixForm[mat];
>  l = Transpose[mat];
>  x = l[[1, All]];
>  z = l[[2, All]];
>
>  Clear[kj];
>   lst = {};
>  For[i = 1, i <= 220, i = i + 1,
>   If[x[[i]] == hg, lst = Append[{z[[i]]}, lst]]];
>  (*For[i=1,i<=220,i=i+1,If[MemberQ[lst,x[[i]]]==True &&x[[i]]=
==j \
> ,lst=Append[{z[[i]]},lst],Delete[lst,i]]];*)
>  kj = Flatten[lst];
>  x1 = kj[[1]];
>  y1 = kj[[2]];
>  y2 = kj[[3]];
>
>  (*Print[MemberQ[kj,y2]]
>  If[MemberQ[kj,y2]== False,Print["you are great"],Print["You are \
> still great"]]
>  If[MemberQ[kj,y2]-> False,Print["you are great"],Print["You are \
> still great"]]*)
>
>  If[MemberQ[kj, x1] == True,
>     If[ MemberQ[kj, y2] == False,
>      plot1 = Flatten[Append[{ListPlot[{x1, y1}]}, plot1]]
>      , plot2 =
>        Flatten[Append[{ListPlot[{{x1, y1}, {x1, y2}}]}, plot2]];
>      ]]
>    myPlots = Join[plot1, plot2];
>  , {m, 2}]
> Show[plot2]
>
> Errors and output
>
> Part::partw: Part All of {} does not exist. >>
> Part::partw: Part All of {} does not exist. >>
> Part::partw: Part 4 of {}[[1,All]] does not exist. >>
> General::stop: Further output of Part::partw will be suppressed during
> this calculation. >>
> Set::write: Tag Times in Null {} is Protected. >>
> Set::write: Tag Times in Null {} is Protected. >>
> Show::shx: No graphical objects to show. >>
> Show[{}]



  • Prev by Date: Re: Re: UNDO and Mathematica - how useless is it?
  • Next by Date: Re: Re: Fast Access to Installed Packages
  • Previous by thread: Show[list] does not work - details of the script
  • Next by thread: dynamic popupmenu help needed