wolfram-mathematica

Getting CellDingbat to remember its state between Mathematica sessions

我的梦境 提交于 2019-11-30 20:05:56
I have modified my notebook's stylesheet to include a StyleData["Todo"] that inherits from StyleData["Item"] . It changes the cell dingbat to a checkbox. In the stylesheet editor: Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Item"]], CellDingbat->DynamicModuleBox[{$CellContext`color$$}, CheckboxBox[ Dynamic[$CellContext`color$$], {RGBColor[1, 0.5, 0],RGBColor[0,Rational[2, 3], 0]}, Background -> Dynamic[$CellContext`color$$]], DynamicModuleValues :> {} ], ] The problem is that the state of the checkbox, when used in a notebook, is not saved between Mathematica sessions. I thought the

Define Custom Notation in Mathematica

限于喜欢 提交于 2019-11-30 19:37:15
问题 I often need to extract to restrict value lists to sublists, ie if vals gives values of vars={x1,x2,x3,x4} , and I need values of svars={x2,x4} I do restrict[list,vars,svars] where restrict[vars_, svars_, vals_] := Extract[vals, Flatten[Position[vars, #] & /@ svars, 1]] I'd like to improve code readability, perhaps by defining following custom notation for restrict[vars,svars,vals] (source: yaroslavvb.com) My questions are What is a good way to implement this? Is this a good idea altogether?

Is it safe to turn off Pattern::patv?

一曲冷凌霜 提交于 2019-11-30 19:29:49
By default Mathematica throws a warning message if I use the same name for both Blank and BlankSequence patterns: f[{x_, ___} | x__] := g[x] Pattern::patv: Name x used for both fixed and variable length patterns. >> Pattern::patv: Name x used for both fixed and variable length patterns. >> Yet the function works as I desire: f[{1, 2, 3}] f[1, 2, 3] g[1] g[1, 2, 3] Therefore, is it safe to use Off[Pattern::patv] and continue as I please? I am aware of a variety of different, more verbose methods that can accomplish the same task, and it is not my desire to analyze their respective merits. I am

(MathLink) Correct handling of Messages generated by slave kernel

怎甘沉沦 提交于 2019-11-30 18:42:33
问题 When working through MathLink with slave kernel I have a problem with correct parsing TextPacket s. In particular when such packet corresponds to a Message generated by the slave kernel I do not understand how to handle it correctly at all. I need such Messages to be printed in the evaluation notebook as if they were generated by master kernel (but with some mark to make clear that it comes from the slave). And I need to separate TextPacket s corresponding to Message s from just to Print[]

Struct data type in Mathematica?

此生再无相见时 提交于 2019-11-30 17:27:20
After playing with Mathematica's symbolic and numerical capabilities, I find it to be a decent programming language, too. However, something making it less appealing as a general-purpose language is the lack of C-like struct data type (or the record type as known in Pascal). How can I get around this problem? sakra Update : Mathematica 10 has introduced Association , which has many of the most important properties of a struct . (See new answer .) The original, somewhat deprecated version of this answer is below. You can use a Mathematica rule lists to mimic a C-like struct data type. E.g.,:

Plotting linear inequalities in Mathematica

你说的曾经没有我的故事 提交于 2019-11-30 17:25:06
问题 I have linear systems of inequalities in 3 variables and I'd like to plot these regions. Ideally, I'd like something that looks like objects in PolyhedronData. I tried RegionPlot3D, but the results are visually poor and too polygon-heavy to rotate in real time Here's what I mean, the code below generates 10 sets of linear constraints and plots them randomCons := Module[{}, hadamard = KroneckerProduct @@ Table[{{1, 1}, {1, -1}}, {3}]; invHad = Inverse[hadamard]; vs = Range[8]; m = mm /@ vs;

What is the best way to find the period of a (repeating) list in Mathematica?

半世苍凉 提交于 2019-11-30 17:12:35
What is the best way to find the period in a repeating list? For example: a = {4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2} has repeat {4, 5, 1, 2, 3} with the remainder {4, 5, 1, 2} matching, but being incomplete. The algorithm should be fast enough to handle longer cases, like so: b = RandomInteger[10000, {100}]; a = Join[b, b, b, b, Take[b, 27]] The algorithm should return $Failed if there is no repeating pattern like above. Szabolcs Please see the comments interspersed with the code on how it works. (* True if a has period p *) testPeriod[p_, a_] := Drop[a, p] === Drop[a, -p] (* are all the

Mathematica editor: Removing the right matching ']' automatically when the left '[' is removed?

半腔热情 提交于 2019-11-30 16:45:39
问题 In Mathematica editor (i.e. notebook), one of the hardest things I always found is when I want to edit some long expression, and go and remove the left end "[" of some expression then before I get the chance to remove the right "]", the code in the cell will get all messed up, since it is no longer balanced, making it really hard to go chase the, now hanging, right end "]" since it is no longer in its original place! Here is a simple example, suppose we have this Text[Row[{PaddedForm

using python to solve a nonlinear equation

送分小仙女□ 提交于 2019-11-30 16:08:50
I have never used python but Mathematica can't handle the equation I am trying to solve. I am trying to solve for the variable "a" of the following equations where s, c, mu, and delta t are known parameters. I tried doing NSolve, Solve, etc in Mathematica but it has been running for an hour with no luck. Since I am not familiar with Python, is there a way I can use Python to solve this equation for a? You're not going to find an analytic solution to these equations because they're transcendental, containing a both inside and outside of a trigonometric function. I think the trouble you're

Test if an expression is a Function?

你离开我真会死。 提交于 2019-11-30 15:55:17
问题 How would a function FunctionQ look like, maybe in a way I can even specify the number of arguments allowed? 回答1: I really feel bad posting after Simon and Daniel, but their codes fail on non-functions which are not symbols. Checking for that and adding a check for builtins via NumericFunction , as suggested by Simon, we arrive at something like FunctionQ[_Function | _InterpolatingFunction | _CompiledFunction] = True; FunctionQ[f_Symbol] := Or[ DownValues[f] =!= {}, MemberQ[ Attributes[f],