From: Murray Eisenberg on
For a start at what's going on, try:

s = x + h;
Manipulate[s^2, {h, 0.1, 1}]

On 7/20/2010 3:42 AM, Sam Takoy wrote:
> Hi,
>
> I find that I don't understand a very basic thing about the building
> blocks of Mathematica.
>
> When the first command is
>
> s = x + h
>
> I figured that (string?) "s" represents (string?) "x+h".
>
> This theory pans out when one tries
>
> s^2
>
> or
>
> D[s, x]
>
> or
>
> Integrate[s, {x, 0, h}]
>
> and
>
> s /. h -> 1
>
> But then by doesn't this work:
>
> Manipulate[ Plot[s, {x, 0, h}], {h, 0.1, 1}]
>
> Many thanks in advance!
>

--
Murray Eisenberg murray(a)math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

From: Sam Takoy on
Thanks to all who responded!

May I belabor this point a little. I understand how to make Manipulate
work and I understand functions. (I am a product of Scheme from with
Mathematica seems to have borrowed a few ideas.)

My question is much more formal: What are the building blocks of
Mathematica, the formal language. When you say

s = x+h

what is s?

Is it an "expression"? Does s represent x+h wherever it appears
(assuming x and h are unassigned at the time of s=x+h)? Apparently not
always: yes in s/.h->5, but not in Manipulate.

So here, then is my "model" of Mathematica:
In s = x+h, s is an "Expression"
In s[x_, h_]:=x+h, s is a "Function"

Manipulate expects a "Function" so that answers my question.

Then what is

s[h_] := x + h? Is it an "Expression" or a "Function" of h with a
parameter x?

Would then Manipulate[Plot[s[h], {x, 0, 1}, PlotRange -> {0, 1}], {h, 0,
1}] work? (The answer is yes.) So apparently, Plot is happy with an
"Expression", but Manipulate wants a "Function"? Why?

Also, in Manipulate[Plot[x+h, {x, 0, 1}, PlotRange -> {0, 1}], {h, 0,
1}], x+h is no longer an "Expression", but is once again a "Function",
because of the context? Even though it's inside Plot which is happy with
an "Expression"?

A personal note: I guess I'm a little frustrated that after a few months
of working with Mathematica, I still have to try things before I know
whether they'll work or not. I'm used to having a clear picture of the
grammar of the language that I'm working with, but I'm struggling here.


On 7/21/2010 7:14 AM, dr DanW wrote:
> I ran into this problem yesterday. I don't know exactly why it
> happens, I think it has something to do with the way Manipulate
> localizes variables. To solve it, I use a trick I found that lets me
> take an expression built up of global symbols and localize the
> symbols. Your trivial example:
>
> s = x + h
>
> Make a function out of it. The Evaluate[] is necessary to evaluate s,
> which replaces it with x+h
>
> sfnc = Function[{x, h}, Evaluate[s]]
>
> Now the Manipulate[] works fine
>
> Manipulate[Plot[sfnc[x, h], {x, 0, h}], {h, 0.1, 1}]
>
> I find myself using this trick a lot.
>
> Regards,
> Daniel
>


From: Bill Rowe on
On 7/22/10 at 5:44 AM, sam.takoy(a)yahoo.com (Sam Takoy) wrote:

>May I belabor this point a little. I understand how to make
>Manipulate work and I understand functions. (I am a product of
>Scheme from with Mathematica seems to have borrowed a few ideas.)

>My question is much more formal: What are the building blocks of
>Mathematica, the formal language. When you say

>s = x+h

>what is s?

>Is it an "expression"?

No, s is a symbol which evaluates to x+h as a result of the
assignment operation.

>Does s represent x+h wherever it appears (assuming x and h are
>unassigned at the time of s=x+h)? Apparently not always: yes in
>s/.h->5, but not in Manipulate.

>So here, then is my "model" of Mathematica: In s = x+h, s is an
>"Expression" In s[x_, h_]:=x+h, s is a "Function"

With s=x+h, x+h is evaluated and becomes one of the OwnValues of
s. With s[x_, h_]:= x+h, x+h is not evaluated and the expression
x+h becomes one of the DownValues for s. That is:

In[5]:= s = x + h;
{OwnValues[s], DownValues[s]}

Out[6]= {{HoldPattern[s] :> h + x}, {}}

In[7]:= Clear[s];
s[x_, h_] := x + h
{OwnValues[s], DownValues[s]}

Out[9]= {{}, {HoldPattern[s[x_, h_]] :> h + x}}

>Manipulate expects a "Function" so that answers my question.

No, the following works just fine.

Manipulate[Plot[x (1 + a x), {x, 0, 6}], {a, 0, 2}]

Here, the argument x (1 + a x) is simply an expression, clearly
not a function with named arguments.

>Then what is

>s[h_] := x + h? Is it an "Expression" or a "Function" of h with a
>parameter x?

Actually, I would call this an example of bad programming style.
The result depends on h which is local to s and x which is a
global parameter. The dependence on a global parameter often
leads to unexpected results and difficult debugging.

>Would then Manipulate[Plot[s[h], {x, 0, 1}, PlotRange -> {0, 1}],
>{h, 0, 1}] work? (The answer is yes.) So apparently, Plot is happy
>with an "Expression", but Manipulate wants a "Function"? Why?

Manipulate isn't restricted to functions.

>Also, in Manipulate[Plot[x+h, {x, 0, 1}, PlotRange -> {0, 1}], {h,
>0, 1}], x+h is no longer an "Expression", but is once again a
>"Function", because of the context? Even though it's inside Plot
>which is happy with an "Expression"?

No, x+h doesn't become a function simply by making it an
argument to Plot.

>A personal note: I guess I'm a little frustrated that after a few
>months of working with Mathematica, I still have to try things
>before I know whether they'll work or not. I'm used to having a
>clear picture of the grammar of the language that I'm working with,
>but I'm struggling here.

I don't have much to say here. I've been using Mathematica since
version 1.2 and still find I need to try some things out before
I know whether they will work as I want. This is particularly
true of newer features like Manipulate that I don't use frequently.


From: Leonid Shifrin on
Hi Sam,

A short answer is that there are no functions in Mathematica, only
expressions and
replacement rules (except pure functions perhaps). When you define a
"function" like

s[x_, h_]:=x+h

you really associate a certain global replacement rule with a symbol <s>. When
evaluator sees a match, it rewrites according to this rule. There are no
stack frames
or activation records - it is a completely different mechanism that
superficially resembles
function calls (there is evaluation stack which is similar but not the
same). So, <s>
is always a Symbol.

Generally, Mathematica at its hart is a rule-based engine, not a functional
language. Functional
layer sits already on top of it. So comparisons with Scheme or Lisp may be
misleading. In
particular, if you study Mathematica evaluation loop, you will see that the
recursive part will be similar
to Lisp (that expressions are evaluated before subexpressions normally), but
the part related
to rule application is not found in Lisp.You may want to check out some
simple account explaining
these things about Mathematica, perhaps some parts of the virtual book or
documentation related to evaluation of expressions. I also dwelt somewhat on
this in my book,

www.mathprogramming-intro.org/

Regarding Manipulate, this behavior is related to scoping (Manipulate is a
scoping
construct and you relationship between <s> and <h> was originally defined
outside
of the scope of Manipulate).

Hope this helps.

Regards,
Leonid



On Thu, Jul 22, 2010 at 1:44 PM, Sam Takoy <sam.takoy(a)yahoo.com> wrote:

> Thanks to all who responded!
>
> May I belabor this point a little. I understand how to make Manipulate
> work and I understand functions. (I am a product of Scheme from with
> Mathematica seems to have borrowed a few ideas.)
>
> My question is much more formal: What are the building blocks of
> Mathematica, the formal language. When you say
>
> s = x+h
>
> what is s?
>
> Is it an "expression"? Does s represent x+h wherever it appears
> (assuming x and h are unassigned at the time of s=x+h)? Apparently not
> always: yes in s/.h->5, but not in Manipulate.
>
> So here, then is my "model" of Mathematica:
> In s = x+h, s is an "Expression"
> In s[x_, h_]:=x+h, s is a "Function"
>
> Manipulate expects a "Function" so that answers my question.
>
> Then what is
>
> s[h_] := x + h? Is it an "Expression" or a "Function" of h with a
> parameter x?
>
> Would then Manipulate[Plot[s[h], {x, 0, 1}, PlotRange -> {0, 1}], {h, 0,
> 1}] work? (The answer is yes.) So apparently, Plot is happy with an
> "Expression", but Manipulate wants a "Function"? Why?
>
> Also, in Manipulate[Plot[x+h, {x, 0, 1}, PlotRange -> {0, 1}], {h, 0,
> 1}], x+h is no longer an "Expression", but is once again a "Function",
> because of the context? Even though it's inside Plot which is happy with
> an "Expression"?
>
> A personal note: I guess I'm a little frustrated that after a few months
> of working with Mathematica, I still have to try things before I know
> whether they'll work or not. I'm used to having a clear picture of the
> grammar of the language that I'm working with, but I'm struggling here.
>
>
> On 7/21/2010 7:14 AM, dr DanW wrote:
> > I ran into this problem yesterday. I don't know exactly why it
> > happens, I think it has something to do with the way Manipulate
> > localizes variables. To solve it, I use a trick I found that lets me
> > take an expression built up of global symbols and localize the
> > symbols. Your trivial example:
> >
> > s = x + h
> >
> > Make a function out of it. The Evaluate[] is necessary to evaluate s,
> > which replaces it with x+h
> >
> > sfnc = Function[{x, h}, Evaluate[s]]
> >
> > Now the Manipulate[] works fine
> >
> > Manipulate[Plot[sfnc[x, h], {x, 0, h}], {h, 0.1, 1}]
> >
> > I find myself using this trick a lot.
> >
> > Regards,
> > Daniel
> >
>
>
>


From: Nasser M. Abbasi on
On 7/22/2010 2:44 AM, Sam Takoy wrote:

>
> A personal note: I guess I'm a little frustrated that after a few months
> of working with Mathematica, I still have to try things before I know
> whether they'll work or not. I'm used to having a clear picture of the
> grammar of the language that I'm working with, but I'm struggling here.
>

Welcome to Mathematica programming :)

In Mathematica, there is no language reference manual. Hence, one must
try something to learn how it works.

Its like you have a large box full of different tools, it takes time to
learn which tool works best with which other tool and the best way to
carry and move each tool.

It is well known that Mathematica has a steep learning curve, and I
think this is the main reason for that.

But it seems that after more time with it, one starts to see common
patterns of how things works and to see the logic behind things, but it
does take time. May be years, not months?

--Nasser



First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: Animated Gif
Next: What's wrong with this assuming?