Hot answers tagged

21

The documentation for MATLAB's mrdivide states that The operators / and \ are related to each other by the equation B/A = (A'\B')' In light of this we may write: LinearSolve[Transpose[A], b] This question was also answered by Daniel Lichtblau here. The equivalency follows from the following two properties of the transpose: $$ \left(A^\mathrm{-1}\...


21

I just stumbled upon it! At least when storing the factorization in a LinearSolveFunction object, we can use it for the transposed solve by supplying a further (not documented?) string variable to it: When calling sol[b, 1234] with a LinearSolveFunction object sol, Mathematica tells us that it only accepts the strings "N", "T", "C", and "J". As it is the ...


14

Let's make this a little more challenging: invThread[body : _[h_[___] ..]] := Replace[Thread[body, h], _[x_ ..] :> x, {1}] f[{a, b}, {x, y}, z]; % // Thread % // invThread {f[a, x, z], f[b, y, z]} f[{a, b}, {x, y}, z]


13

Henrik has already written on the undocumented second argument of a LinearSolveFunction[], so let me just put out a short demo wrapper showing how to use the built-in, but undocumented LAPACK routines to solve a linear equation, given the output of LUDecomposition[]: Options[backsub] = {Mode -> Automatic}; backsub[lu_, perm_, opts : OptionsPattern[]][...


12

Here's a nice one-liner: TransformationFunction[{{1, 0, 0, 0}, {0, 0, -1, 0}, {0, 1, 0, 3}, {0, 0, 0, 1}}] // InverseFunction TransformationFunction[{{1, 0, 0, 0}, {0, 0, 1, -3}, {0, -1, 0, 0}, {0, 0, 0, 1}}] Note that TransformationFunction[] is the head of the results returned by geometric *Transform functions, which take a homogeneous transformation ...


12

The identity does not hold for x < -1: Plot[ProductLog[x*Exp[x]], {x, -5, 5}] FullSimplify[ProductLog[x*Exp[x]], x >= -1] x (* result in 10.1.0 under Windows *)


11

If you have a homogenous transformation matrix of the form $$\begin{bmatrix} \mathrm{R_{3 \times 3}} & \mathrm{d}_{3 \times 1} \\ 0_{1\times 3} & 1_{1\times 1} \end{bmatrix}$$ Then the inverse is given by $$\begin{bmatrix} \mathrm{R}^{-1} & -\mathrm{R}^{-1}\mathrm{d} \\ 0 & 1 \end{bmatrix}$$ Therefore, if your ...


11

This is a little long for a comment, beside which it points out an unexpected difference probably resulting from subsystems evolving independently at different times. In addition to Henrik's comment that the code is uncompilable, there are two things to add. The most significant is how the quantity is computed, and the other is the usual difference between ...


10

Clear[RaisedCosineDistribution] As with built-in distributions, you need to include the parameters in the distribution definition, and the constraints on the parameters as Assumptions in the ProbabilityDistribution RaisedCosineDistribution[μ_, s_] = ProbabilityDistribution[ 1/(2 s) (1 + Cos[((x - μ)/s) π]), {x, μ - s, μ + s}, Assumptions -> {...


9

The determinant computation is a matter of memory use in terms of how much we want to store for subdeterminants of a Laplace expansion. Mathematica simply refuses to go that route after 11x11. YOu can do your own as below. myDet[mat_] /; Length[mat] <= 4 := Det[mat] myDet[mat_] := myDet[mat] = Sum[mat[[1, j]]*myDet[Drop[mat, {1}, {j}]], {j, Length[...


9

Let me demonstrate two approaches in this answer. P.J. Acklam (WayBack archive of his page) devised a not-too-long method to approximate the quantile of the Gaussian distribution, with absolute errors on the order of $10^{-9}$. I have tweaked the Mathematica implementation of Acklam's approximation by William Shaw so that one can obtain a compilable ...


8

I think you're on the right track with Reduce. If the domain Reals is specified, Reduce will return results that can be converted to a Piecewise expression straightforwardly. Clear[f]; f[t_] = Piecewise[{{t/4, t < 0}, {t/2, t < 3}, {3/2 + (t - 3)*3, True}}]; invPW[f_] := Evaluate @ Module[{t}, Piecewise[ List @@ Reduce[# == f[t], t, Reals] /. {...


8

InverseFunction operates on an abstract function, yielding an abstract, anonymous function. g[x] is not a function, but a formula. Just leave out [x]. On 11.3, MacOS: InverseFunction[g] (* 2 Log[1/2 (-#1 - Sqrt[4 + #1^2])] & *) Unfortunately, although it's an inverse, this doesn't appear to be the branch you want. More: You can get @Henrik ...


7

This seems usable at least for moderately large x. one could use cutoffs and different start values if this is not useful in smaller ranges. f[x_?NumberQ] := y /. FindRoot[LogIntegral[y] == x, {y, x*Log[x]}, WorkingPrecision -> 20, PrecisionGoal -> 12] Two examples: f[10^200] (* Out[55]= 4.6565831394119416907*10^202 *) NIntegrate[n/(f[n])^2, ...


7

The easiest way is to evaluate this: SetOptions[EvaluationNotebook[], StyleDefinitions -> Notebook[{ Cell[StyleData[StyleDefinitions -> "Default.nb"]], Cell[StyleData[All], FontColor -> GrayLevel[1], Background -> GrayLevel[0]]}, Visible -> False, StyleDefinitions -> "PrivateStylesheetFormatting.nb"] ] However as you ...


7

You can plot curves defined by implicit equations using ContourPlot: ContourPlot[ Cos[Sqrt[y]] + Sin[Sqrt[y]]/Sqrt[y] == Cos[x], {x, 0, 10}, {y, 0, 10}]


7

I don't know that you can do it in a single command. But it's easy enough to do in two. Say your list is list={2,1,4}; x = ConstantArray[0, Max[list]]; x[[list]] = 1; x {1, 1, 0, 1} Effectively, Part (or the shortcut [[ ]]) acts as an approximate inverse of Position.


7

I like to use NDSolve to find inverses when symbolic methods won't work. The ODE corresponding to the inverse of a function g can be obtained by differentiating the defining equation: eqn = g[ginv[h]] == h; D[eqn, h] g'[ginv[h]] ginv'[h] == 1 So, to find the inverse using NDSolve, we need the above equation and an initial point. For example: ginv = ...


7

FullSimplify[ComplexExpand[ArcSin[(2 x)/(1 + x^2)], TargetFunctions->{Re, Im}], x ∈ Reals] Integrate[% /. Abs -> RealAbs, x] % // Simplify Resolve[ForAll[x, Evaluate[ArcSin[(2 x)/(1 + x^2)] == D[%, x]]], Reals] ArcTan[Abs[-1 + x^2], 2 x] $\begin{array}{cc} \{ & \begin{array}{cc} x \tan ^{-1}\left(x^2-1,2 x\right)+\log \left(x^2+1\right) &...


7

Out of curiosity, I tried to write my own version of inverse CDF for the normal distribution. I employ a qualitative approximation of the inverse CDF as initial guess and apply Newton iterations with line search until convergence. This is the code: f[x_] := CDF[NormalDistribution[0, 1], x]; finv[y_] := InverseCDF[NormalDistribution[0, 1], y]; p = 1/200; q =...


7

Inverse functions can be plotted using ParametricPlot or ParametricPlot3D Clear["Global`*"] F[alpha_, x_] = Assuming[{Element[x, Reals], -1 < x < 1}, Integrate[1/Sqrt[(x^2 - 1)^2 + alpha*x], x] // FullSimplify]; Manipulate[ ParametricPlot[{ {Re@F[alpha, x], x}, {Im@F[alpha, x], x}, {Abs@F[alpha, x], x}}, {x, -1, 1}, ...


6

As suggested by george2079 the equation can be solved by substition: Reverse@Simplify[f[x] == 2 x + 1/x - f[1/x]/2 /. f[1/x] -> 2/x + x - f[x]/2] f[x] == 2 x To find $f^{-1}(4)$ you can use f = 2 # &; InverseFunction[f][4] 2


6

Basic implementation Here is a function BlockTridiagonalSolve that takes three lists of blocks (diag, lower and upper) and a list of vector pieces (vec) and solves the corresponding linear system: BlockTridiagonalSolve[diag_?(ArrayQ[#, 3] &), lower_?(ArrayQ[#, 3] &), upper_?(ArrayQ[#, 3] &), vec_?MatrixQ] := Module[{a, i, n = Length[diag], d =...


6

define a function of two variables, f[x_, m_] = 2 m (Sqrt[4 x^2 (1 - m^2) + m^4 + 4 m^2 x]/(1 - m^2) + m^2/(4 (1 - m^2) Sqrt[m^2 - 1]) ArcSin[(2 (1 - m^2) x + 4 m^2)/(4 m^3)]); then tell InverseFunction to invert w.r.t. the first argument: inv = InverseFunction[f, 1, 2]; Show[{Plot[f[x, 2], {x, -10, 10}, PlotRange -> All], ListPlot[...


6

PowerExpand[ProductLog[x Exp[x]]] x This assumes $x\ge0$


6

Indeed, the numerical inversion of the DSolve solution, s = DSolveValue[{r'[t] == r[t]^2/3 (1 - r[t]), r[0] == 2}, r[t], t] // Simplify (* InverseFunction[Log[1 - #1] - Log[#1] + 1/#1 &][1/2 + I π - t/3 - Log[2]] *) used to plot r as a function of t in the question is having difficulties. An alternative approach is to plot t as a function of r. First,...


6

It could also be view as a precision problem: Plot[ InverseFunction[Log[1 - #1] - Log[#1] + 1/#1 &][1/2 + I π - t/3 - Log[2]], {t, 0, 20}, PlotRange -> All, PlotPoints -> 100, WorkingPrecision -> 50, Frame -> True] Of course InverseFunction is much slower than @bbgodfrey's parametric approach.


6

Normal@SparseArray[Thread[{1, 5} -> 1]] (* {1, 0, 0, 0, 1} *) Normal@SparseArray[Thread[{2, 1, 4} -> 1]] (* {1, 1, 0, 1} *)


6

Your function g is equal to -2 Sinh[x/2]: g[x_] := E^(-x/2) - E^(x/2); FullSimplify[-2 Sinh[x/2] == g[x]] True So you are looking for ginv[y_] := -2 ArcSinh[y/2]


6

Here's another way to get the correct inverse function using Solve, which allows me to specify that the function is defined on the real axis: Clear[x, y]; g[x_] := E^(-x/2) - E^(x/2) gInverse[y_] = ExpToTrig[x /. First@Solve[g[x] == y, x, Reals]] (* ==> -2 ArcSinh[y/2] *)


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