Somos
  • Member for 3 years, 1 month
  • Last seen this week
How do I apply something like the double slash notation but not at the end of line?
9 votes

The problem, as usual, is precedence. You need to use parenthesis to group expressions. The code Scan[ (#^2 // Print) &, {1, 2, 3}] will now do what you want. Your reference to "mathematica pipe ...

View answer
If and summation: why do I have the index of the summation in the final result?
8 votes

The other answer by Andrew technically answers your question, but, maybe what you really wanted was Sum[If[b =!= 0, a[m], 0], {m, 1, 4}] which returns a[1] + a[2] + a[3] + a[4] The difference ...

View answer
How to use Intersection[] but keep the original list order
7 votes

One way to keep the order in l1 is this: l1 = {"qwe", "abc", "abb", "aba", "ddd"}; l2 = {"abc", "abd", "aba", "qwe&...

View answer
GeometricMean definition
7 votes

Similar to many other means, the geometric mean is homogenous. This means that GeometricMean[ c data ] == c GeometricMean[ data ] should be true for any number c. However, the problem is that $n$th ...

View answer
Replacement Rule for Functions
7 votes

I think you have found one of the quirks of the convoluted Mathematica evaluation rules. I suggest you try instead the alternative code: D[f @@ {t. x, y, z}, x] /. {dd_[nn__][f][xx__] -> dd[nn][g][...

View answer
Mathematica code for computing the $p$-adic expansion of rational numbers
Accepted answer
7 votes

Something like this code should work: pExpand[x : (_Rational | _Integer), p_?PrimeQ, n_ /; Positive[n]] := Module[{q = p^n, num, den, v, e}, v = IntegerExponent[#, p] & /@ ({num, den} = #[x] ...

View answer
Is there a way to omit the "In[1]:=" prompt?
Accepted answer
6 votes

You may be able to use some code like this repl[prompt_: ""] := Module[{input}, Print[]; While[True, input = InputString[prompt]; If[input == "quit", Break[], Print[]; Print["Out[", ++$Line, "]...

View answer
How to convert a real number to a string only in the original length?
5 votes

The following function seems to do what you want realToString[x_Real] := StringReplace[ToString[x,InputForm], StringExpression[a__~~"`"~~__~~"*^"~~b__]->a~~"*^"~~b];...

View answer
Why is this product equal to zero, when the correct result is 2+GoldenRatio?
5 votes

The problem seems to be that the function f[n_, x_:(Sqrt[5]-1)/2] := Product[ 1 / (1 - x^k/(1 - x^(2*k))), {k, 2, n}]; when called with f[n, x] returns -((-1 + x + x^2)*QPochhammer[-1, x, 1 + n]*...

View answer
How to extract coefficients of a generating function like this one, using a computer?
5 votes

Define the function a[n_, k_] := SeriesCoefficient[ Sum[x^i, {i, 0, k}]^10, {x, 0, n}]; and you want a[3 k, k]. By the way, this is a $9$th degree polynomial function as given by ...

View answer
Why doesn't Mathematica evaluate the series with recursion relationship as expected?
5 votes

A simple method uses a Rule[] to replace g'[t] with its value: ClearAll[x, g, dg]; dg[t_, 0] := g[t]; dg[t_, i_Integer /; i > 0] := D[dg[t, i - 1], t] /. g'[t] -> (t - x) g[t]; As a ...

View answer
Composition of polymorphic functions
Accepted answer
5 votes

Try the code f[{a_, b_}] := {a^2, b^2}; f[c_Integer] := c^4; g[x: _Integer | {a_, b_}] := f@f@x; g /@ {2, {x, y}, 3.5} // InputForm which returns the result {65536, {x^4, y^4}, g[3.5]} which is ...

View answer
Special Character boxtime
5 votes

The code \:22A0 will produce the Unicode character for BOX TIMES but the font used for output must support the Unicode character and not all of them do, otherwise an empty "Box" (or something similar) ...

View answer
Does Mathematica gives us a wrong result for the integral of a function including elliptic functions?
5 votes

This is just a guess, but the problem seems to be that$\sqrt{x^2}\ne x$ in general. The integral according to Mathematica had to choose a square root and it is wrong half of the time. More precisely, ...

View answer
How to display very small numbers in Mathematica?
5 votes

The simplest methods are usually the best. Try this code N[Cos[x] - Exp[-x 27/10] /. x -> 17*^-26, 15] // InputForm or a minor variation With[{x = 17*^-26}, N[Cos[x] - Exp[-x 27/10], 15]] // ...

View answer
Possible bug in Solve function?
Accepted answer
5 votes

Thanks for asking! In version 9.0 only 16 solutions are returned and they are all valid. In version 10.2 there are 20 solutions, with the extra 4 all being invalid. Contragulations! I think you found ...

View answer
Calculating the series expansion of a theta function
Accepted answer
5 votes

Try the following code with an example: QP[a_, q_] := QPochhammer[a, q]; (* T[x, q] == Product[(1 - x q^k) (1 - q/x q^k), {k, 0, Infinity}] *) T[x_, q_] := QP[x, q] QP[q/x, q]; U[a_, x_, Q_] := With[{...

View answer
Show that all coefficients in a recursively defined function are integers
4 votes

You asked Any suggestions on an elegant, computational approach? It is not clear what you mean by that. Since this was a Putnam problem, the elegant approach would be to observe that $\,Q_n(x)=U_n(x/...

View answer
Why does FunctionPeriod give two different results for alternative forms of the given function?
4 votes

The FunctionPeriod function is fragile. For example, testing for four variants With[{f = Sin[a - x] Sin[x + a]}, FunctionPeriod[#, x] & /@ {f, f // Simplify, 2 f, 2 f // Simplify}] returns {2*Pi,...

View answer
Problem defining a function that takes a binary input to its decimal form
Accepted answer
4 votes

The code binarytodecimal[b_String] := NumberForm[ToExpression["2^^"<>b], 20]; does what you want. For example binarytodecimal["0.0100101010101010101010"] evaluates to 0....

View answer
Change TeXForm of ArcTan
4 votes

Usually the simplest methods tend to be the best. Try the code expr = ArcTan[x/y] - ArcTan[z/w]; StringReplace[ToString[expr, TeXForm], "\\tan ^{-1}" -> "\\arctan"] which returns \arctan\left(\...

View answer
Recursive Function in Multiple Variables
Accepted answer
4 votes

(*" This code follows the Division Polynomial Wikipedia article "*) ClearAll[s, d, psi, phi, omega, P, x, y, X, Y, Z, A, B]; s[0] = d[0] = 0; d[1] = d[2] = 1; d[3] = Y; d[4] = Z; s[n_ /; n < 0] := -...

View answer
What is the definition of Curl in Mathematica?
4 votes

I suggest to modify your simple piece of code to make the output more informative. I define a function dim[] which is similar to the Dimensions[] function but is restricted to matrices and arrays. For ...

View answer
Can pattern assumptions be made reliable?
4 votes

The following code: Simplify[expr, {f[a] > 0, f[b] > 0}] // InputForm returns (d*f[b])^(p1 - p2)*(f[a]*f[c])^(p1 + p2) which seems to be what you wanted. Assumptions in general are not ...

View answer
Define different actions for the same operator depending on the nature of its arguments
Accepted answer
4 votes

If I understand your question correctly, then the ** can be given a definition to do what you want. This code may work for you Unprotect[NonCommutativeMultiply]; ClearAll[NonCommutativeMultiply]; ...

View answer
Create special matrix
4 votes

dij[i_, j_] := If[i < j, 0, Mod[i - j, 2]] 4 i / If[j == 0, 2, 1]; d[n_] := Table[dij[i, j], {i, 0, n}, {j, 0, n}]; The function d[] returns a Mathematica matrix as a list of lists as you would ...

View answer
Find n-roots of unity and put them in vector
Accepted answer
4 votes

The following function returns a list of all the $n$th roots of unity in order. nth[n_] := Table[Exp[k 2 Pi I/n], {k, 0, n-1}]; You can do it with the more general code nth[n_] := Table[Root[#1^n - ...

View answer
Why does Solve produce a result in terms of one variable, but not the other?
3 votes

Try the code poly = x^4 + a x^3 + b x^2 + a x + 1; soln = Solve[Discriminant[poly, x] == 0]; Print[soln // InputForm] Print[poly /. soln // Factor // InputForm] (* {{b -> -2 - 2*a}, {b -> -2 + 2*...

View answer
What has DiscreteAsymptotic been thinking about for so long?
3 votes

I am running Mathematica version 12.3.0 for Microsoft Windows. The code Table[Integrate[Product[x-k, {k, 1, n}], {x, 0, 1}], {n, 8}] //InputForm returns {-1/2, 5/6, -9/4, 251/30, -475/12, 19087/...

View answer
Force EllipticTheta to "use" $(e^{\pi i\tau})^{\lambda}=e^{\pi i\tau \lambda}$
Accepted answer
3 votes

From the Functions Wolfram site we get EllipticTheta[2, z, q] == (q^(1/4) EllipticTheta[3, z - (Pi/2) τ, q])/ E^(I z) /; q == E^(I Pi τ) So define a new theta Th2 and test the identity with the ...

View answer
1
2 3 4 5 6