Message Boards Message Boards

0
|
8492 Views
|
8 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Wierd output when working with Module function

Posted 4 years ago

Hello everyone

Why does the outputs of these:

t=1;
x0 = Table[i, {i, 1, 100}];
y0 = Table[Sin[(20 + i)/20 t], {i, 0, 99}];
data = N[Table[{x0[[i]], y0[[i]]}, {i, 1, 100}]]
  ListPlot[data]

and this:

Module[
{t = 1},
 x0 = Table[i, {i, 1, 100}];
 y0 = Table[Sin[(20 + i)/20 t], {i, 0, 99}];
 data = N[Table[{x0[[i]], y0[[i]]}, {i, 1, 100}]]
   ListPlot[data]
 ]

are different?

The latter output is weird, it is a list of ListPlots, and that is not what I am looking for.

POSTED BY: Ehud Behar
8 Replies

I'll add my 2 cents of answer relating to your sentence

Thanks, but I rather want them to be returned on two separate output lines, if that's possible.

As mentioned by Szabolcs functions always return a single answer. Therefore, if you want more than one answer in a single return, you need to return a single answer that can contain multiple elements, and this was already mentioned earlier, with several suggestions for different containers.

If you need to create a cell, you can print (Szabolcs already mentioned that) of your more fancy cell generation using CellPrint and related functionality. Anyway, printing or generating cells is considered as Side Effect of the function, since it is not the value returned by the function, but "something" that is done "on the side" (just compare Map to Scan to notice the difference)

I hope it helps to summarize the issue

sincerely

Szabolcs is a bona fide Mathematica expert and I am reluctant to post after him.

Nevertheless, here is a way that may help you understand how you might want to use Dynamic to achieve the display-both.

Dynamic[{data, listPlot}]
Module[{t = 1}, x0 = Table[i, {i, 1, 100}];
 y0 = Table[Sin[(20 + i)/20 t], {i, 0, 3}];
 data = N[Table[{x0[[i]], y0[[i]]}, {i, 1, 3}]];
 listPlot = ListPlot[data];
 ]

But, because x0,y0 aren't needed after the Module, they should probably be local to the Module. Try this and look at what happened to the Dynamic output above.

functionThatUsesModule[t_, length_] :=
  Module[{x0, y0}, 
  x0 = Table[i, {i, 1, length}];
  y0 = Table[Sin[(20 + i)/20 t], {i, 0, length}];
  data = N[Table[{x0[[i]], y0[[i]]}, {i, 1, length}]];
  listPlot = ListPlot[data];
  ]

functionThatUsesModule[3, 5]

calling the function again:

functionThatUsesModule[6,12]
POSTED BY: W. Craig Carter

You are conflating output lines you see during interactive work with return values of functions.

Functions are building blocks of programs. So is the Module construct (which is technically a function in Mathematica). They return precisely one value. The purpose of functions is to be able to engineer a larger program in a structured, readable and maintainable manner.

If you are working interactively, and want to see the output right away, then you do not need Module.

If you are looking for a command that explicitly instruct the system to output something into a notebook, and can be used as part of a larger program, see Print.

POSTED BY: Szabolcs Horvát

Because you forgot a ; at the end of the data = ... line. The newline (i.e. whitespace) after that line will be interpreted as multiplication. The system actually warns you: it displays a $\times$ sign.

POSTED BY: Szabolcs Horvát
Posted 4 years ago

Thanks! That's right. Actually there is no grey multiplication sign on my notebook, and that was my source for confusion.

Another question then arises:

Is there any way to use Module and still show the output of two different commands, as I tried to do on the original post?

POSTED BY: Ehud Behar
Posted 4 years ago

You could change the last expression in the Module to

{data, ListPlot[data]}
POSTED BY: Rohit Namjoshi
Posted 4 years ago

Thanks, but I rather want them to be returned on two separate output lines, if that's possible.

POSTED BY: Ehud Behar
Posted 4 years ago

I use Column[], Row[] and Grid[] to provide me cleaner output. In your case, since you want the outputs to be on separate rows, you can do something like this:

values = someFunctionThatReturnsAFlatList[]; 
Column[values]

The first line gets the values, but suppresses the output. The second line puts each item of the list returned on its own line.

POSTED BY: Mike Besso
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract