wolfram-mathematica

How to improve ( mathematica ) performance when dealing with recursive functions?

时间秒杀一切 提交于 2019-12-10 18:59:43
问题 Background. I want to print a table of convergents for 31^(1/2). I made the following recursive definition of the table. ( Exchange 31^(1/2) with the golden ratio and the table below would contain the Fibonacci series ). cf := ContinuedFraction tf := TableForm p[-1] = 0; p[0] = 1; q[-1] = 1; q[0] = 0; a[k_] := cf[Sqrt[31], k][[k]] p[k_] := a[k]*p[k - 1] + p[k - 2] q[k_] := a[k]*q[k - 1] + q[k - 2] s[n_] := Timing[Table[{k, a[k], p[k], q[k]}, {k, 8, 8 n, 8}]] // tf Timing increases

SymPy equivalent to HoldForm in mathematica

非 Y 不嫁゛ 提交于 2019-12-10 18:54:43
问题 In Mathematica, it is possible to prevent the system from simplifying expressions when they are entered. The syntax is as follows: HoldForm[x/x] Is it possible to do something similar with SymPy? 回答1: The following approaches achieve a similar effect. There might be others available I am not aware of. import sympy as sp x = sp.symbols('x') expr1 = x/x expr2 = sp.S('x/x', evaluate=False) expr3 = sp.Mul(x, 1/x, evaluate=False) print(expr1) print(expr2) print(expr3) 1 x/x x/x 回答2: You can also

how to overload Times and Plus for matrix multiplication in mathematica

时光毁灭记忆、已成空白 提交于 2019-12-10 18:29:54
问题 I want to overload Times and Plus for matrix multiplication in mathematica, for example, let Times be BitAnd , and Plus be BitOr , then do the matrix multiplication. Is there anyway to do this in a simple way, without rewriting my own matrix multiplication? Thanks. 回答1: The question is what you want to alter - the behavior of Times and Plus , or Dot . Generally, Block trick is often the simplest way. In this case, since Dot does not call high-level Plus or Times , you can do: mat1 = {{1,2},{3

How to define constants for use with With[] in one place and then apply them later?

折月煮酒 提交于 2019-12-10 17:36:37
问题 I like to use With[] with constants that I need to use in 2 different places. Instead of typing the same long list of constants in 2 places, I have been trying to figure how to use a variable for this list, and then use this variable in the few places I want to use the list. The problem is that I need to Hold the list and then ReleaseHold it later when time to use it, but I can't get this part right. (tried many things, nothing working for me) Here is an example: With[{$age = 2, $salary = 3},

Mathematica clear a function's derivative definition

匆匆过客 提交于 2019-12-10 16:27:29
问题 I defined the derivative of a function in Mathematica without defining the function itself, i.e. I have a function definition that looks like this: y'[x_] := constant * f'[x]. I can't figure out how to clear it out. If I use Clear[y'] or `ClearAll[y'], I get an error message: ClearAll::ssym: y' is not a symbol or a string. Clear[y] and ClearAll[y] do nothing to remove the definition of y' . Any ideas on how I can remove the definition of y' ? 回答1: This should do what you want: y'[x_] =. See

Making customized InputForm and ShortInputForm

你说的曾经没有我的故事 提交于 2019-12-10 15:43:37
问题 I often wish to see the internal representation of Mathematica 's graphical objects not in the FullForm but in much more readable InputForm having the ability to select parts of the code by double-clicking on it and easily copy this code to a new input Cell . But the default InputForm does not allow this since InputForm is displayed by default as a String , not as Mathematica 's code. Is there a way to have InputForm displayed as Mathematica 's code? I also often wish to see a shortened

Conditional coloring based on a gradient

て烟熏妆下的殇ゞ 提交于 2019-12-10 15:31:15
问题 Please consider : Manipulate[ Row[{ Graphics[Disk[]], Graphics[{ Polygon[{{0, 0}, {3, 0}, {3, 1}, {0, 1}}, VertexColors -> {White, Blend[{White, Blue}], Blend[{White, Blue}], White}], Black, Thick, Line[{{i, 0}, {i, 1}}]}, ImageSize -> 300]}], {i, 0, 3}] Using Szabolcs`s solution on Gradient Filling How could I color the disk with the color located underneath the Black Line ? 回答1: Here is one solution which works because the color on the left is White and the gradient is linear. With[{max = 3

Custom Intervals of Markers in Mathematica PlotMarkers

為{幸葍}努か 提交于 2019-12-10 14:56:04
问题 I am trying to plot multiple lists in the same plot in Mathematica (ListLinePlot) and use PlotMarkers and the PlotLegend Package to get the final figure. The issue is that Mathematica puts a marker for every point and this makes it hard to tell which marker is where in the plot. Is it possible to have a plot marker appear every n sample (e.g. every 10 points for a 100 point plot). The Directive at the moment is PlotMarkers->{Automatic, Small}. 回答1: If you want more control over the location

Prohibit replacement by ReplaceAll (/.)

最后都变了- 提交于 2019-12-10 14:43:57
问题 From Mathematica's own documentation: {g[1],Hold[g[1]]}/.g[n_]:>n+1 leads to {2, Hold[1 + 1]} My question: is there a way to protect subexpressions from being replaced by ReplaceAll? I am using composite constructs as variables, like v[a, b] and would like to be able to do stuff like this v[a, b] + a - b /. {a -> x, b -> y} leading to v[a, b] + x - y and not v[x, y] + x - y without complicated patterns. Unfortunately, using Replace and level specifications is not option. This idiom v[a, b] +

Controlling measure zero sets of solutions with Manipulate. A case study

情到浓时终转凉″ 提交于 2019-12-10 14:29:11
问题 To address the question we start with the following toy model problem being here just a case study: Given two circles on a plane (its centers (c1 and c2) and radii (r1 and r2)) as well as a positive number r3, find all circles with radii = r3 (i.e all points c3 being centers of circles with radii = r3) tangent (externally and internally) to given two circles. In general, depending on Circle[c1,r1], Circle[c2,r2] and r3 there are 0,1,2,...8 possible solutions. A typical case with 8 solutions :