Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7085 > unrolled thread

C/Fortran-like #include functionality for large expressions?

Started byFrank Iannarilli <frankeye@cox.net>
First post2011-12-10 12:29 +0000
Last post2011-12-11 08:46 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  C/Fortran-like #include functionality for large expressions? Frank Iannarilli <frankeye@cox.net> - 2011-12-10 12:29 +0000
    Re: C/Fortran-like #include functionality for large expressions? David Bailey <dave@removedbailey.co.uk> - 2011-12-11 08:57 +0000
    Re: C/Fortran-like #include functionality for large expressions? "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-12-11 08:46 +0000

#7085 — C/Fortran-like #include functionality for large expressions?

FromFrank Iannarilli <frankeye@cox.net>
Date2011-12-10 12:29 +0000
SubjectC/Fortran-like #include functionality for large expressions?
Message-ID<jbvjbn$isb$1@smc.vnet.net>
Hi All,


Is there any C #include or similarly Fortran INCLUDE functionality I can gain within Mathematica?  The purpose is to enhance readability of very large function definitions or Manipulate[] invocations, by splicing-in "chunks" of code (expressions) defined in separate cells.

Here's an example of an attempt that doesn't work:

testInclude[] :=
 Module[{a}, a = 2.; ToExpression[p];  Print[a]]

p = "a=3."     // The expression chunk to "include"

testInclude[] 
// would like this to return "3", but stuck returning "2". 

Thanks,
Frank

[toc] | [next] | [standalone]


#7121

FromDavid Bailey <dave@removedbailey.co.uk>
Date2011-12-11 08:57 +0000
Message-ID<jc1ral$t3r$1@smc.vnet.net>
In reply to#7085
On 10/12/2011 12:29, Frank Iannarilli wrote:
> Hi All,
>
>
> Is there any C #include or similarly Fortran INCLUDE functionality I can gain within Mathematica?  The purpose is to enhance readability of very large function definitions or Manipulate[] invocations, by splicing-in "chunks" of code (expressions) defined in separate cells.
>
> Here's an example of an attempt that doesn't work:
>
> testInclude[] :=
>   Module[{a}, a = 2.; ToExpression[p];  Print[a]]
>
> p = "a=3."     // The expression chunk to "include"
>
> testInclude[]
> // would like this to return "3", but stuck returning "2".
>
> Thanks,
> Frank
>
>
Your example didn't work because a is local to the Module, so in effect 
you executed a line of the form:

a$17=2.;ToExpression["a=3."];Print[a$17]

(The number 17 is arbitrary - see the help for the Module function)

If the expression that you want to include does not contain variables 
that should be local, the solution is trivial - just assign them to a 
variable:

subst=Hold[a=3.]

(no need to convert to string) and insert the expression as needed in 
your function as ReleaseHold[subst].

Getting the replacement a -> a$17 would be a lot harder, and is 
essentially pointless because a should presumably be global.

David Bailey
http://www.dbaileyconsultancy.co.uk

[toc] | [prev] | [next] | [standalone]


#7133

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-12-11 08:46 +0000
Message-ID<jc1qli$ss5$1@smc.vnet.net>
In reply to#7085
On Sat, 10 Dec 2011 12:29:43 -0000, Frank Iannarilli <frankeye@cox.net>  
wrote:

> Hi All,
>
>
> Is there any C #include or similarly Fortran INCLUDE functionality I can  
> gain within Mathematica?  The purpose is to enhance readability of very  
> large function definitions or Manipulate[] invocations, by splicing-in  
> "chunks" of code (expressions) defined in separate cells.
>
> Here's an example of an attempt that doesn't work:
>
> testInclude[] :=
>  Module[{a}, a = 2.; ToExpression[p];  Print[a]]
>
> p = "a=3."     // The expression chunk to "include"
>
> testInclude[]
> // would like this to return "3", but stuck returning "2".
>
> Thanks,
> Frank
>

It is possible, but not with Module, because Module creates new local  
variables under different names (so a always refers to Global`a). You can  
use Block instead to get the behaviour you want. The string/ToExpression  
indirection is not needed, and personally I dislike delayed definitions  
being made using an unnecessary function call pattern as required in other  
languages, so I changed these too:

testInclude := Block[{a}, a = 2.; p; Print[a]]

testInclude

prints:

2.

Now, we try

p := a = 3; testInclude

which prints:

3.

(you can get 2. again by evaluating p =. to clear the "include".)

Also note that the C++ comment syntax "// comment" has a different meaning  
in Mathematica, so I would encourage you to use Mathematica "(* comment  
*)" comments in code examples.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web