# Tag Info

34

You can use custom transformation rules, for example: -11 - 2 x + x^2 - 4 y + y^2 - 6 z + z^2 //. (a : _ : 1)*s_Symbol^2 + (b : _ : 1)*s_ + rest__ :> a (s + b/(2 a))^2 - b^2/(4 a) + rest returns (* -25 + (-1 + x)^2 + (-2 + y)^2 + (-3 + z)^2 *) The above rule does not account for cases where b is zero, but those are easy to add too, if ...

17

The following seems to work, however I think it's not general enough: At a clean nb, enter: For[i = 0, i < 4, i++, Print[{i, {33, i}}]] For[i = 0, i < 4, i++, Print[Graphics[Circle[], ImageSize -> 20]]] And then retrieve the Print[ ] output as: c = Cases[NotebookRead /@ Cells[GeneratedCell -> True], Cell[___, "Print", ___]]; ToExpression ...

15

That's an interesting first question. Welcome. :-) From a simplistic perspective this should work, but as you observe there are evaluation properties that are more complex. Here is a reference for most (but not all) behavior: The Standard Evaluation Sequence Let's follow those steps for your example. Heads are evaluated first Evaluate the head h of ...

15

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 ...

14

Case #1 Observe: "anything" /. Plus[___] -> "match" "match" This is because Plus[___] evaluates to ___, and ___ matches anything. You can use HoldPattern: Sqrt[Plus[x, y]] /. HoldPattern[Plus[___]] -> u Sqrt[u] Case #2 You must understand that pattern matching is done on something close to the FullForm of an expression, rather than the ...

14

I assume you have Maple to use. If so, Simply open Maple and type the Mathematica command itself directly into Maple using the FromMma package built-into Maple, like this: restart; with(MmaTranslator); #load the package (*[FromMma, FromMmaNotebook, Mma, MmaToMaple]*) and now can use it FromMma(Integrate[Cos[x],x]); One can also use Maple convert ...

9

This is a rather simple-minded approach, but maybe it will be useful to you: ClearAll[opCount]; opCount[h_, expr_] := Cases[ Hold[expr], HoldPattern@h[args : _ ~Repeated~ {2, Infinity}] :> Length@Hold[args] - 1, -2 ] // Total; SetAttributes[opCount, HoldRest]; Let's try: opCount[Times, 1*2*3*4*5 + 6*7*8] (* -> 6 *) opCount[Plus, 1*2*3*4*5 + ...

9

Here's one way to count the number of multiplications in an expression (equal to or greater than the number of Times in the expression). It should also work for several other binary operators, Listable or not (although I haven't tested it on them). t[x_, oper_: Times] := Tr @ ((Length[#] - 1) & /@ (Extract[x, {Sequence @@ Drop[#, ...

8

Assuming you don't have any built-in symbols in that list, you could simply do: DeleteDuplicates@Cases[Leff, _Symbol, Infinity] (* {da, ma, dm, mc, La, h, R} *) If you do have symbols from built-in contexts or packages, you can simply pick out only those that are in the Global context with: With[{globalQ = Context@# === "Global" &}, ...

8

For the first puzzle, I can only guess. The idea is that Function with named variables is a true lexical scoping construct, in that it cares about the possible name collisions inside the inner scoping constructs, including another Function-s (this is where it is different from Slot- based functions, which are not like that. The price to pay is that ...

7

Here is one possibility. Comparing patterns is a special case of comparing trees in general, so we need some specialized version of diff of Mathematica expressions. One way to build such a diff is to "serialize" expression into atomic elements and then compare the serialized forms. I will use slightly more complex patterns as examples: a = ...

7

Please let me know if this is moving in the right direction: expr = a + b*3*c + d; Replace[expr, h_[x___] :> {x}, {0, -1}] {a, {3, b, c}, d} Given that heads are lost here, perhaps you want something like: Replace[expr, h_[x___] :> {h, x}, {0, -1}] {Plus, a, {Times, 3, b, c}, d} If this is close to what you a related question that you ...

7

Convert Maple expressions to Mathematica: Through latex： Through free form input：

6

One thing that may help is to make it a univariate function in a new variable t and extract derivatives. Below I changed your code to remove a space between vector and z on the last line or so. With[{fexpt = fexp /. Thread[vectorz -> t*vectorz]}, c = fexpt /. t -> 0; bvec = With[{der = D[fexpt, t] /. t -> 0}, Map[D[der, #] &, vectorz]]; amat ...

6

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"]]; SystemDumpshowStringDiff[notebook1, notebook2]

6

One possible compact solution: multCount[expr_] := Total[Length /@ List @@@ Cases[expr, _Times, \[Infinity]] - 1] multCount[a b c + d e] (* 3 *) multCount[a*b*c + (d*e)/(f*g*h - i*j*k*l)] (* 10 *) Notice that since Mathematica represents divisions by multiplications with the multiplicative inverse and subtractions by additions with the number multiplied ...

6

As of version 9 this has changed http://reference.wolfram.com/mathematica/Compatibility/tutorial/Utilities/FilterOptions.html So this package needs to use FilterRules. I Downloaded this package, and changed Summa.m according to the above, and now it loads ok. Changed every place it said FilterOptions to Sequence@@FilterRules. Run few tests from the ...

6

Maybe we can learn something from what Compile produces. cf = Compile[ {{w, _Real}, {y, _Real}} , w - 4 (w - y) ((w - y)^2 y + 6 (1 + y) ((w - y) y + (1 + y)^2))/((w - y)^2 ((w - y) y + 6 y^2 + 8 y (1 + y)) + (1 + y)^2 (36 (w - y) y + 24 (1 + y)^2)) ] In the compiled code we see that at least the number of ...

6

You can use FullSimplify and play with the ComplexityFunction Option until you obtain a satisfactory result. For example: Let's define our function in terms of LeafCount c[n_][e_] := n Count[e, _Sin | _ArcTan, Infinity] + LeafCount[e] Then: FullSimplify[Sin[1/2 ArcTan[(2 Log[5])/(Log[5]^2 - 2)]], ComplexityFunction -> c[#]] & /@ Range[40, 60, ...

6

You can use Normal, ConditionalExpression is not explicitly mentioned there but documentation says it deals with special forms. p1 = y /. {First[Solve[x^2 + y^2 + x == 1, y, Reals]]} // First ConditionalExpression[-Sqrt[1 - x - x^2], 1/2 (-1 - Sqrt[5]) < x < 1/2 (-1 + Sqrt[5])] Normal @ p1 -Sqrt[1 - x - x^2]

5

CoefficientRules seems to do a good job. Its results have to be transformed into actual arrays afterwards, but the timing is fast. I am confident that those with greater facility in pattern manipulation can simplify this code, but it does work, albeit inelegantly. Most of the effort lies in constructing the matrix. The two forms of ij transmute ...

5

The documentation for FullSimplify lists the following transformations in the section Examples → Properties & Relations (I do not think this the list is complete): Expand, TrigExpand, PiecewiseExpand, FunctionExpand, LogicalExpand, Factor, FactorSquareFree, TrigFactor, RootReduce, ToRadicals, Together, Apart. It also mentions that PowerExpand makes ...

5

I'm not sure what you think x[t,th]/.sol[[1]] does. From your question, Here is what I think you're trying to accomplish. sol = Flatten[ DSolve[{D[x[t, th], {t, 2}] == -0.2*D[x[t, th], t]/2.30, Derivative[1, 0][x][0, th] == 10.8*Cos[th], x[0, th] == 0}, x[t, th], t]]; x[t_, th_] = sol[[1, 2]]; (* This defines the function using the solution from ...

5

The function you need is TrigToExp, e.g. TrigToExp @ ArcTan[x] 1/2 I Log[1 - I x] - 1/2 I Log[1 + I x] There is an inverse function for TrigToExp, namely ExpToTrig ExpToTrig[ 1/2 I Log[1 - I x] - 1/2 I Log[1 + I x]] ArcTan[x] They both are Listable: Attributes @ {ExpToTrig, TrigToExp} {{Listable, Protected}, {Listable, Protected}} ...

5

This is only a hack, but maybe it just gives you short way out of this. Lately, we had a similar discussion in chat about NValues where the problem was related. It this cases Rojo wanted to use NValues to prevent some of the arguments to stay untouched by N. There too, the problem was when N was called from very outside and dived into the subexpressions ...

5

Analysis It indeed looks like a borderline bug to me. Let us see what is happening. The first observation here is that part assignment does its job all right: sparse = SparseArray[Range[10]]; sparse[[2]] = Sequence[0, 0, 0]; so that ?sparse Global`sparse sparse=SparseArray[Automatic, {10},0,{1,{{0,10},{{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}}}, ...

5

On Leonid's valued opinion that the Close was inappropriate I have reopened this question. By my interpretation this does what is requested: SetAttributes[setSpec, HoldAllComplete] setSpec[s_Symbol, spec__] := s /: h_[pre__, s, post___] := h[pre, spec, post] The usage is: setSpec[ops1, Filling -> {1 -> {2}}, PlotStyle -> {Red, Green}, Frame ...

5

Here is another way using Evaluate: ops1 = {Filling -> {1 -> {2}}, PlotStyle -> {Red, Green}, Frame -> True}; Plot[{Sin[x] + x/2, Sin[x] + x}, {x, 0, 10} , Evaluate@ops1 , PlotLabel -> Style["Using Evaluate", 20] ]

5

In our case a simple and direct approach would be defining a list of rules. Here is an example: rules = { c_ Sum[n a[n] c_^(n-1), {n, 0, Infinity}] :> Sum[n c^n a[n], {n, 0, Infinity}], α_ Sum[a[n] c_^n, {n, 0, Infinity}] + Sum[n a[n] c_^n, {n, 0, Infinity}] :> Sum[(α + n) a[n] c^n, {n, 0, Infinity}]}; Let's define an appropriate ...

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