Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: a question about [[ ]]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg46629] Re: a question about [[ ]]
  • From: drbob at bigfoot.com (Bobby R. Treat)
  • Date: Thu, 26 Feb 2004 17:53:18 -0500 (EST)
  • References: <c1h028$9p6$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Possibly Ted Ersek's HoldTemporary package will satisfy. You can find
that at MathSource:

http://library.wolfram.com/infocenter/MathSource/705/

Needs["Ersek`HoldTemporary`"]
Clear[f, g]
f[{x_, y_}] := {f[x], f[y]}
g[x_] /; Length[f[x]] > 1 := {f[x][[1]], f[x][[2]]}
g[x_] := HoldTemporary[Take[f[x], 2]]

(* or g[x_] := HoldTemporary[{f[x][[1]], f[x][[2]]}] *)

g[a]
{f[a][[1]], f[a][[2]]}

g[{x, y}]
{f[x], f[y]}

f[a_] := {a, a^2}
g[a]
{a, a^2}

g[{x, y}]
{{x, x^2}, {y, y^2}}

There's not a lot you can DO with g[a] while f[a] is undefined, but
HoldTemporary has options that may help.

Bobby

Paolo Bientinesi <pauldj at cs.utexas.edu> wrote in message news:<c1h028$9p6$1 at smc.vnet.net>...
> Hello,
> here is what I am trying to do:
> 
> g is a function that invokes f.
> f[x] is undefined,
> while f[{x,y}] returns a list, say {f[x],f[y]}  (or any other list).  
> 
> 
> Assume 
> f[{x_, y_}] := { f[x], f[y] }
> and
> g[x_] := {f[x][[1]], f[x][[2]]}
> 
> 
> I would like to have
> 
> In := g[a] 
> Out:= {f[a][[1]], f[a][[2]]}
> 
> and 
> 
> In := g[{b,c}] 
> Out:= {f[b], f[c]}
> 
> 
> Rationale: function g has the knowledge that f iv evaluated return a list.
> If f is not evaluated I want the symbolic expression to be returned.
> 
> 
> The problem is the first case: g[a].
> I cannot prevent Mathematica from evaluating f[a][[1]] 
> returning the unwanted a. 
> For f[a][[2]] the situation is slightly better because after the mesg
> Part::"partw": "Part \!\(2\) of \!\(f[a]\) does not exist."
> Mathematica return the unevaluated expression f[a][[2]].
> 
> 
> Basically I would like Mathematica to evaluate (Something)[[ ]] 
> only is Something is a list and leave everything symbolic otherwise.
> 
> 
> Thanks for any suggestion.


  • Prev by Date: Re: Question about Integrate and If
  • Next by Date: RE: Matrix Element Extraction
  • Previous by thread: a question about [[ ]]
  • Next by thread: Inversion using Cholesky Decomposition