wolfram-mathematica

How do I replace each value in run with the integer preceding the run

守給你的承諾、 提交于 2019-12-08 16:23:23
问题 Using Mathematica, I have a list: l={0,0,0,1,2,0,0,0,1,0,0,0,2,0,0,0} I want to apply a function to the above list to obtain the following: {0,0,0,1,2,2,2,2,1,1,1,1,2,2,2,2} Essentially I want to replace the runs of 0 values with runs of the same length, but using the value of the positive integer just preceding each run of 0s. I thought I could do this easily with FoldList, but I can't see my way through to a solution. Many thanks. 回答1: Here is your test list: tst = {0, 0, 0, 1, 2, 0, 0, 0,

Printing symbol name and value in Mathematica

Deadly 提交于 2019-12-08 15:46:22
问题 I'd like to create a function My`Print[args__] that prints the names of the symbols that I pass it, along with their values. The problem is that before the symbols are passed to My`Print , they're evaluated. So My`Print never gets to see the symbol names. One solution is to surround every argument that I pass to My`Print with Unevaluated[] , but this seems messy. Is there a way of defining a MACRO such that when I type My`Print[args__] , the Mathematica Kernel sees My`Print[Unevaluated /@

Testing for Null and not Null in Mathematica

我只是一个虾纸丫 提交于 2019-12-08 15:11:33
问题 What is the best / cleanest / advisable way to test if a value is Null in Mathematica ? And Not Null? For example: a = Null b = 0; f[n_] := If[n == Null, 1, 2] f[a] f[b] has the result: 1 If[0 == Null, 1, 2] Where I would have expected that 2 for f[b]. 回答1: As pointed out by Daniel (and explained in Leonid's book) Null == 0 does not evaluate to either True or False , so the If statement (as written) also does not evaluate. Null is a special Symbol that does not display in output, but in all

CheckAbort inside MathLink functions?

醉酒当歌 提交于 2019-12-08 15:11:03
问题 I just found that such MathLink functions as LinkWrite and LinkRead have something like its own internal CheckAbort that absorbs any aborts, and does not propagate them further. This can be easily shown with LinkRead : link = LinkLaunch[First[$CommandLine] <> " -mathlink"]; LinkRead[link]; LinkWrite[link, Unevaluated[Pause[10]]]; {LinkRead[link], Print["!!"]} After evaluating the above code press Alt + . and you will get the following output: During evaluation of In[6]:= !! Out[9]=

serialize runtime objects into svcutil generated partial class representations

一个人想着一个人 提交于 2019-12-08 14:17:26
I use svcutil to generate the following code: //------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // Runtime Version:4.0.30319.239 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace System.Net { using System.Runtime.Serialization; [System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime

Finding the Maximum

随声附和 提交于 2019-12-08 13:59:39
问题 How to find the following Maximum or supremum by computer software such as Mathematica and Matlab: $\sup\frac{(1+s)^{4}+(s+t)^{4}+t^{4}}{1+s^{4}+t^{4}}$ ? Instead of numerical approximation, what is the accurate maximum? Thanks. 回答1: Since the question seems a bit like homework, here's an answer that starts a bit like a lecture: ask yourself what happens to the function as s and t go to small and to large positive and negative values; this will help you to identify the range of values you

serialize runtime objects into svcutil generated partial class representations

Deadly 提交于 2019-12-08 07:28:43
问题 I use svcutil to generate the following code: //------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // Runtime Version:4.0.30319.239 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace System.Net { using System.Runtime.Serialization; [System

Count the number of heads per set in Mathematica

主宰稳场 提交于 2019-12-08 04:03:35
问题 First of all, thank you for all your responces. I am doing my best to keep up with all the suggestions. However I'm trying to stay on track. So now I have s = Table[RandomChoice[{Heads, Tails}, 2 i + 1], {i, 10}]; Next, I want to count the number of "Heads" that occur for each "i". I can do this for one case....say 5, n = RandomChoice[{Heads, Tails}, 5]; n1 = Count[n, Heads]; But I am having trouble expanding this.... Thanks again. p.s. what are down votes? Too easy? 回答1: Map ( /@ ) the

How to check whether the FrontEnd considers evaluation still running?

浪子不回头ぞ 提交于 2019-12-08 03:56:50
问题 Is there a way to check programmatically whether the FrontEnd considers evaluation still running? Or even better: is there a way to check whether the FrontEnd has some pending inputs to be sent to the kernel? P.S. This question has arisen from previous question. EDIT When evaluating a Cell in the FrontEnd we usually create a queue of inputs for the kernel. I need a function that will return True if the FrontEnd has sent to the kernel the last input of the queue of inputs from the

Follow up to: TransformedDistribution in Mathematica

ぐ巨炮叔叔 提交于 2019-12-08 03:15:07
问题 I have a follow up question to Sasha's answer of my earlier question at TransformedDistribution in Mathematica. As I already accepted the answer a while back, I thought it made sense to ask this as a new question. As part of the answer Sasha defined 2 functions: LogNormalStableCDF[{alpha_, beta_, gamma_, sigma_, delta_}, x_Real] := Block[{u}, NExpectation[ CDF[StableDistribution[alpha, beta, gamma, sigma], (x - delta)/u], u \[Distributed] LogNormalDistribution[Log[gamma], sigma]]]