Tag Info

Hot answers tagged

12

You can use And and still get what you want: x := 1 If[And @@ {True, False, True, False, (x := 2; True)}, Print["Yes"], Print["No"]]; x (* No 2 *) What happens is that the List and its arguments are evaluated before And is applied to it, hence setting the value of x.


11

Implementation The following implementation is based on expression serialization and SequenceAlignment built-in function. The idea is to break expressions into constituent parts, then align these part sequences, and then determine the positions where the expressions are different. The auxiliary heads we will need are inert heads diff and myHold, the latter ...


10

The idea The idea is that if we have $\log(a+b),\qquad a\gg b$ , then we can equivalently write this as $\log a + \log(1 + b/a)$ and the second part will be small, so that one can first compare the first part(s). The power towers with base numbers larger than 1 naturally lead to such logarithms when we repeatedly take the $\log$ of them. So, there ...


10

Here's one idea. Hold the expression unevaluated and go up the expression tree from (near) the bottom, level by level, and evaluate. expr = HoldForm[1/((a + 2 b)/c^2)] /. {a -> 1, b -> 2, c -> 3} out = ToExpression@ToString[FullForm@#] & /@ (ReplacePart[expr, # -> Extract[expr, #] & /@ #] & /@ ...


9

You can use Defer to see how to properly enter your "summation" type notation. Defer[1 + Sum[Sum[1/((k + 2) k!), {k, n, Infinity}], {n, 0, Infinity}]] You can then enter that output to see that it works. You must've entered something different.


5

Indeed, Nest and NestList do not support functions with Hold attributes (as well as Fold and FoldList, etc). There were discussions of this in the past. I was able to find one such. As far as I can tell, this is by design. What happens is that NestList (for example) maintains an internal list of intermediate results, the last of which is used in the next ...


5

If you can convert expressions to text form, there's a possible answer here. I sometimes use it to compare notebooks: notebook1 = StringJoin[ Import["/tmp/freaky-illusion.nb", "Plaintext"]]; notebook2 = StringJoin[ Import["/tmp/freaky-illusion-1.nb", "Plaintext"]]; System`Dump`showStringDiff[notebook1, notebook2]


4

Independently I arrived at something similar to Michael's answer, yet different. I borrowed his formatting function after seeing it as it works better than what I had. Perhaps this will also be of use: evalFromBottom[expr_, lv_: 1] := If[lv > Depth@expr, expr, With[{ev = Replace[expr, x_ :> RuleCondition[x], {-lv}]}, If[expr === ev, ...


4

As Matariki pointed out in comments, this is a simple syntax error. The infix version of Or in Mathematica is ||, not |. Change this, like so: Xhcmaleorfemale[maleorfemale_, h_] := Which[(h < 3 || h > 7), 0, (h > 2 && h < 8), Which[maleorfemale == 0, Xhcmale, maleorfemale == 1, Xhcfemale][[h - 2]]]; And you will get ...


3

Perhaps something along these lines could help? $PrePrint= # /. Except[Null] :> With[{line = $Line}, DynamicSetting@Dynamic@In[line]] /. DownValues[In] &; After running this, everything you evaluate is automatically wrapped in Dynamic so its value is updated automatically. I haven't tested it much so there may be issues. (Be careful ...


3

The answer isn't so much related to Map or Table, but to Unevalauted and the evaluation sequence. The first one Map[Unevaluated, {1, 2}] (* {Unevaluated[1], Unevaluated[2]} *) All the heads and arguments are inert, and none has heads Evaluate or Unevaluated to worry about. Note that the symbol Unevaluated doesn't have head Unevaluated. Just apply the ...


3

This seems rather convoluted, and there is almost certainly an easier way to approach whatever it is you are wishing to do, but I like answering questions like this as it allows working with more unusual aspects of the language. We can do this: Block[{tmp, Part}, Hold @@ {test[2]}] /. _[x_] :> (x = {111, 222}); As with my previous answer we may wish ...


3

One approach is to turn your expression into functions. I give a few extra variations just to show what can be done and which you might find useful to learn. Here's the whole thing as a function (to be used, for example, in your Plot3D): Bfn = Evaluate[B /. {x -> #1, y -> #2}] &; Bfn[x, y] == B (* True *) Component functions can be ...


3

You can perform a replace: B[[2,3]]/.{x->-1,y->0} If you have a table of x and y values (call it xyvals) for given matrix elements then you could do: Table[B[[m,n]]/.{x->xyvals[[m,n,1]],y->xyvals[[m,n,2]]},{m,Length[B]},{n,Length[B[[1]]]}]


3

Jacob, allow me to suggest a presentation that I think you will find relevant and informative: Working with Unevaluated Expressions - Robby Villegas Some excerpts: Unevaluated is a wrapper on arguments that is simply a signal to the evaluator to avoid evaluating the argument. ... It is transparent to the function receiving the argument. You can ...


2

For diff'ing code fragments/expressions, you can copy-and-paste as "Plain Text" into Quick Diff (online) or into WinMerge (PC-based), ref. http://stackoverflow.com/q/15655828/879601 (also mentions a Mac-based method using Bash). E.g. WinMerge:- (For diff'ing packages and notebooks I favour CSDiff.)


2

As we should expect the following identity (maybe under some certain mathematical assumptions, I'm not sure): $$\sum _i \sum _j f(i,j)=\sum _i \left(\sum _j f(i,j)\right)\;\text{,}$$ which we can confirm in Mathematica 9 by the examples say: However, the nested-iterator version of the summation in the original question takes forever time in my ...


2

I think it's because in the sigma form the two sums are treated as a double sum with different order as the expression case: In==> $$ \text{Hold}[\sum _{n=0}^{\infty } \sum _{k=n}^{\infty } \frac{1}{(k+2) k!}]//\text{InputForm} $$ Out==> Hold[Sum[1/((k + 2)*k!), {n, 0, Infinity}, {k, n, Infinity}]] and according to the documentation, the sum for n will ...


1

update[x_] := (x[[1]] = x[[1]]*2; x) Attributes[update] = HoldAll; list = Range[3]; That first evaluation of list in Nest[update, list, 5] is an issue but it's not the main issue. If that was the issue it could be solved with Nest[update, Unevaluated@list,5]. However, this would only work in the first iteration. Nest applies the function to the result of ...



Only top voted, non community-wiki answers of a minimum length are eligible