A67 ==== Subject: Urgent I need help solving the Laplace equation in three dimensions using Mathematica. Due to the geometric configuration I used oblate and prolate spheroidal coordinates. You can see the equations I'm using in: http://mathematica.no.sapo.pt/index.html ==== Subject: Is Apart[ .. ] the correct way for denominator splitting? When I ask for: Apart[2/(x^2-1), x] I nicely get: 1/(x-1) - 1/(x+1) But unfortunately Apart[2/(2x^2-1), x] does not give the same splitting of the denominator ( it just gives back 2/(2x^2-1) ). What is the correct way to see the denominator splitting with all terms first order (as one would use for integration, etc.) I found Andrzej Kozlowski's answer in message from 2001, but I was wondering whether in the mean time a built-in function might have been added? ==== Subject: Re: Can't assign value to symbols Bruce The only way I can imagine of dealing with this situation is if you first make sure that the data that are originally imported into Mathematica are imported as a string and not an expression. (This is easy to do with external data with Mathematica functions ReadList or Import ). So, for example, if to start with T has the form T = {{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}; Then all you need to do is: Scan[Clear, StringCases[T, LetterCharacter]] and all the prior values of a, b, and c will b cleared. You can then convert T to Expression with ToExpression. Andrzej > Andrzej > I've moved your Scan to the inside-end of the loop, since it kills > two birds with one stone. The code below works as intended unless > a,b,c have values (and then error messages abound). The only way I know to avoid this problem is to make Scan > [Clear,Names[Global`*]] the new first line...except that this > (unfortunately) zaps ALL variables. There must be a better way (than my above idea) to clear the T[[1]]- > variables (that may have values coming into the code below). How would you tell Mathematica to hold unevaluated the T[[1]]- > elements so that they can be used to clear the associated variables? Bruce T={{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}; > X=Map[SymbolName,T[[1]]]; Do[Print@MapThread[Set,{ToExpression[X,StandardForm,Unevaluated],T > [[k]]}]; > Scan[Clear,X], > {k,2,Length@T}]; > Cc: mathgroup@smc.vnet.net ==== > Subject: Re: > Can't assign value to symbols I see. In this case, does not Scan[Clear,X] after the Do loop do what you want? Andrzej > Andrzej >> The aim is to explictly avoid (throughout the code) referring to >> the variables. >> For instance, suppose matrix T (in my code) springs from an Excel >> spreadsheet imported into Mathematica, one whose top row declares >> the variable names, and whose later rows give the variables' values >> for each iteration in a simulation. Suppose further that the >> number of columns may change. >> Bruce >> Bruce >> ==== >> Subject: Can't >> assign value to symbols >> To tell the truth, I don't understand what you are trying to do. Why >> do you want to reevaluate the line X=... ? >> If you do not want at the end of your Do loop the variables a,b,c to >> have assigned values the simplest way is to use Block: >> Block[{a, b, c}, Do[ >> Print@MapThread[Set, {ToExpression[X, >> StandardForm, Unevaluated], T[[k]]}], >> {k, 2, Length@T}]]; >> All the assignments will now take place locally inside Block and the >> variables {a,b,c} are automatically cleared on exit-ting Block. >> Andrzej Kozlowski > Andrzej Using your reply, I offer this code for Lee's consideration. Although it works when first run, if re-run without quitting the > kernal, the X= line causes an error because a,b,c are > instantiated. At the start of the program, how can I clear these variables > WITHOUT explicitly declaring them? I've failed to find the right > argument for Clear (e.g., Clear@Subscript...can't find the right > counterpart for Subscript). > Bruce > T={{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}; X=Map[SymbolName,T[[1]]]; Do[Apply[Clear,ToExpression[X,StandardForm,Unevaluated]]; > Print@MapThread[Set,{ToExpression[X,StandardForm,Unevaluated],T > [[k]]}], > {k,2,Length@T}]; > ==== > Subject: Can't > assign value > to symbols Instead of ToExpression use ToExpression[#, StandardForm, Unevaluated] & For example: > In[1]:= > parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; In[2]:= > MapThread[(#1 = #2) & , {ToExpression[#,StandardForm,Unevaluated] > & /@ > parameters[[1]], > parameters[[3]]}] ; In[3]:= > a Out[3]= > 4 In[4]:= > MapThread[(#1 = #2) & , {ToExpression[#,StandardForm,Unevaluated] > & /@ > parameters[[1]], > parameters[[2]]}] ; In[5]:= > a Out[5]= > 1 > Of course instead of Standardform you can use TraditionalForm or > even > InputForm. Andrzej Kozlowski Chiba, Japan >> but >> now realize there is one additional problem. Once I've executed >> the >> statement MapThread[(#1 = #2) & , {parameters[[1]], parameters >> [[3]]}] , >> because I've specified the first row of parameters as symbols, >> this >> row now loses the symbol names and takes on the assigned values, >> such >> that my attempt to execute the MapThread Statement again for a >> different >> set of values, e.g. parameters[[2]], fails. >> in: MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] ; >> in: {a,b,c} >> Out: {4,5,6} >> in: MapThread[(#1 = #2) & , {parameters[[1]], parameters[[2]]}] ; >> out: Set:: setraw : Cannot assign to raw object 4 ... etc >> I want to preserve the first row of parameters as the symbol >> names, so >> one solution is to use strings, and modify David Park's solution to >> turn >> the strings into symbol names when assigned, This works the first >> time >> it is run, and parameters[[1]] still contains the strings, but ut >> still >> fails the second time. but why? >> in: parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, >> 9}}; >> in: MapThread[(#1 = #2) & , {ToExpression /@ parameters[[1]], >> parameters[[3]]}] ; >> in: {a,b,c} >> out {4,5,6} >> in: MapThread[(#1 = #2) & , {ToExpression /@ parameters[[1]], >> parameters[[3]]}] ; >> out: Set:: setraw : Cannot assign to raw object 4 ... etc >> why does the MapThread statement fail the second time? It seems >> ToExpression/@parameters[[1]] converts the strings a, b, c to >> symbols but then evaluates them prior to returning them, so the >> attempt >> to assign to them in MapThread fails. >> How can I modify the MapThread statement so I can call it multiple >> times, each time assigning values to the symbols identified by the >> strings in the first row?? >> Lee >>> Lee, >>>> Set (=) has a higher precedence Than Function (&). So all you have >>> to do is >>> add parentheses. >>>> parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; >>>> MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] >>> {4, 5, 6} >>>> David Park >>> djmp@earthlink.net >>> http://home.earthlink.net/~djmp/ >>>>>>>>>>>> Situation: I have a table that contains parameters and sets of >>> values >>> that I want to assign to the parameters for the purposes of >>> running a >>> simulation. >>> - parameters = {{a,b,c},{1,2,3},{4,5,6},{7,8,9}} >>> - want to assign values in a given row to the symbols listed in >>> the >>> first row. >>> - tried using: MapThread[ #1 = #2 &, {parameters[[1]], >>> parameters >>> [[3]]} ] >>> - fails with error Tag Slot in #1 is Protected >>> - tried adding Unprotect[#1] and a variety of other attemps, but >>> can't >>> get it to work. >>>> Anyone know how might accomplish this? >>>> Lee >>>>>>>>> > ==== Subject: Re: display of function > The first equation below shows a function for u(y) which is > the sum of the products of pi times yi. In the following steps, I > assign values of p and y. How can I get Mathematica to display the > numerical value of u for the given p and y values? When I insert the > u[y] command, I just get a repeat of the equation I have entered. u[y_] := Sum[Subscript[p, i]*Subscript[y, i], {i, 1, n}] p = {.3,.4,.3} y={10000, 20000, 30000} Darrell, As others have mentioned, for this case the simplest approach is to use Dot: u[y_] := p.y Again, as others have mentioned, using Part instead of Subscript in your method would work. I just want to point out that it's possible to use subscripts in the way you intended: u[y_] := Sum[ p * y ,{i,1,n}] [[i]] [[i]] The above is not working Mathematica code, you need to use 2D boxform input, i.e., input the subscripts using Ctrl-_. In other words, when Mathematica parses 2D boxform input where the subscript is a part specfication, then Part is automatically used instead of Subscript. You can make things even prettier by using [LeftDoubleBracket] and [RightDoubleBracket] instead of [[ and ]]. Carl Woll Wolfram Research ==== Subject: Re: Any ideas about solving an underdetermined system? Mathematica has a method of given the mininum-length solution. Let (x1, x2, ...) be a solution, I want that there are as many X-es > equal to 0. In other words, I want to solving a underdetermined system in which > many unknowns might be 0. And if I known which ones are 0, then the > system may be simplified to a overdetermined one. Any ideas? I'm not sure I understand your question. I presume you mean you are obtaining solutions to an underdetermined linear problem by PseudoInverse. If you set specific xi to 0 that's equivalent to removing those columns from your coefficient matrix. So you could just use Part or Take to remove those columns, solve as usual then insert zeroes at the appropriate location in the solution. Ssezi ==== Subject: Re: Re: Can't assign value to symbols Andrzej I've moved your Scan to the inside-end of the loop, since it kills two birds with one stone. The code below works as intended unless a,b,c have values (and then error messages abound). The only way I know to avoid this problem is to make Scan[Clear,Names[Global`*]] the new first line...except that this (unfortunately) zaps ALL variables. There must be a better way (than my above idea) to clear the T[[1]]-variables (that may have values coming into the code below). How would you tell Mathematica to hold unevaluated the T[[1]]-elements so that they can be used to clear the associated variables? Bruce T={{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}; X=Map[SymbolName,T[[1]]]; Do[Print@MapThread[Set,{ToExpression[X,StandardForm,Unevaluated],T[[k]]}]; Scan[Clear,X], {k,2,Length@T}]; n ==== Subject: Re: Can't assign value to symbols I see. In this case, does not Scan[Clear,X] after the Do loop do what you want? Andrzej > Andrzej The aim is to explictly avoid (throughout the code) referring to > the variables. For instance, suppose matrix T (in my code) springs from an Excel > spreadsheet imported into Mathematica, one whose top row declares > the variable names, and whose later rows give the variables' values > for each iteration in a simulation. Suppose further that the > number of columns may change. Bruce > Bruce > ==== > Subject: Can't > assign value to symbols To tell the truth, I don't understand what you are trying to do. Why > do you want to reevaluate the line X=... ? If you do not want at the end of your Do loop the variables a,b,c to > have assigned values the simplest way is to use Block: Block[{a, b, c}, Do[ > Print@MapThread[Set, {ToExpression[X, > StandardForm, Unevaluated], T[[k]]}], > {k, 2, Length@T}]]; All the assignments will now take place locally inside Block and the > variables {a,b,c} are automatically cleared on exit-ting Block. Andrzej Kozlowski >> Andrzej >> Using your reply, I offer this code for Lee's consideration. >> Although it works when first run, if re-run without quitting the >> kernal, the X= line causes an error because a,b,c are instantiated. >> At the start of the program, how can I clear these variables >> WITHOUT explicitly declaring them? I've failed to find the right >> argument for Clear (e.g., Clear@Subscript...can't find the right >> counterpart for Subscript). >> Bruce >> T={{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}; >> X=Map[SymbolName,T[[1]]]; >> Do[Apply[Clear,ToExpression[X,StandardForm,Unevaluated]]; >> Print@MapThread[Set,{ToExpression[X,StandardForm,Unevaluated],T >> [[k]]}], >> {k,2,Length@T}]; >> ==== >> Subject: Can't >> assign value >> to symbols >> Instead of ToExpression use >> ToExpression[#, StandardForm, Unevaluated] & >> For example: >> In[1]:= >> parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; >> In[2]:= >> MapThread[(#1 = #2) & , {ToExpression[#,StandardForm,Unevaluated]& /@ >> parameters[[1]], >> parameters[[3]]}] ; >> In[3]:= >> a >> Out[3]= >> 4 >> In[4]:= >> MapThread[(#1 = #2) & , {ToExpression[#,StandardForm,Unevaluated]& /@ >> parameters[[1]], >> parameters[[2]]}] ; >> In[5]:= >> a >> Out[5]= >> 1 >> Of course instead of Standardform you can use TraditionalForm or even >> InputForm. >> Andrzej Kozlowski >> Chiba, Japan > but > now realize there is one additional problem. Once I've executed the > statement MapThread[(#1 = #2) & , {parameters[[1]], parameters > [[3]]}] , > because I've specified the first row of parameters as symbols, > this > row now loses the symbol names and takes on the assigned values, > such > that my attempt to execute the MapThread Statement again for a > different > set of values, e.g. parameters[[2]], fails. in: MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] ; > in: {a,b,c} > Out: {4,5,6} > in: MapThread[(#1 = #2) & , {parameters[[1]], parameters[[2]]}] ; > out: Set:: setraw : Cannot assign to raw object 4 ... etc I want to preserve the first row of parameters as the symbol > names, so > one solution is to use strings, and modify David Park's solution to > turn > the strings into symbol names when assigned, This works the first > time > it is run, and parameters[[1]] still contains the strings, but ut > still > fails the second time. but why? > in: parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, > 9}}; > in: MapThread[(#1 = #2) & , {ToExpression /@ parameters[[1]], > parameters[[3]]}] ; > in: {a,b,c} > out {4,5,6} > in: MapThread[(#1 = #2) & , {ToExpression /@ parameters[[1]], > parameters[[3]]}] ; > out: Set:: setraw : Cannot assign to raw object 4 ... etc why does the MapThread statement fail the second time? It seems > ToExpression/@parameters[[1]] converts the strings a, b, c to > symbols but then evaluates them prior to returning them, so the > attempt > to assign to them in MapThread fails. How can I modify the MapThread statement so I can call it multiple > times, each time assigning values to the symbols identified by the > strings in the first row?? Lee >> Lee, >> Set (=) has a higher precedence Than Function (&). So all you have >> to do is >> add parentheses. >> parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; >> MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] >> {4, 5, 6} >> David Park >> djmp@earthlink.net >> http://home.earthlink.net/~djmp/ > >> Situation: I have a table that contains parameters and sets of >> values >> that I want to assign to the parameters for the purposes of >> running a >> simulation. >> - parameters = {{a,b,c},{1,2,3},{4,5,6},{7,8,9}} >> - want to assign values in a given row to the symbols listed in the >> first row. >> - tried using: MapThread[ #1 = #2 &, {parameters[[1]], parameters >> [[3]]} ] >> - fails with error Tag Slot in #1 is Protected >> - tried adding Unprotect[#1] and a variety of other attemps, but >> can't >> get it to work. >> Anyone know how might accomplish this? >> Lee > > ==== Subject: Re: how to find n in expression x^n using a pattern? > >>>I am learning patterns in Mathemtica, and I thought this will >>>be easy. >>>>suppose you are given a factored expression, such as x^n(x^2+2) >>>(x^3 +4) >>>where 'n' above can be any number, including zero (i.e. there is no >>>(x) as a separate factor). I need to find 'n', which can be >>>0,1,2,3,..etc.. >>>>i.e I need to find the power of the factor x by itself if it >>>exist in >>>the expression. not (x^n+anything), just (x^n) byitself. >>>>For example >>>>p= x (x^2+2) ---> here n=1 >>>>I tried this >>>>p /. x^n_(any_) -> n >>>>This did not work since x^1 does not match pattern x^n_. I >>>would have >>>worked if I had x^2 (x^3+2) for example. >>>>I know that I need to use something like x^_:1 to make it >>>match x, >>>which >>>is really x^1, but I do not know how to use it in a replacement >>>rule, >>>becuase >>>when I write >>>>p /. (x^n_:1)(any_) -> n >>>>it is an error. >>>>If I write >>>>p /. (x^_:1)(any_) -> _ >>>>I do not get back anything. >>>>Next I tried the Cases command, and again had no luck. >>>>The main problem is that Mathematica makes a difference between >>>x, x^1, and x^2, etc... when it comes to matching with pattern x^n_ >>>>any idea how to do this? >>>steve >>>>>>>Why not do something like this , it sounds quite simplistic but >>it works :) >>Clear[p] >>p[x_] = x ^5(x^3 + 2x^2 + 3x - 13) >>sol2 = Solve[p[x] == 0, x] >>y = x /. sol2 >>n = Count[y, 0] >>-- >>Pratik Desai >>Graduate Student >>UMBC >>Department of Mechanical Engineering >>Phone: 410 455 8134 > >In fact after seeing this posting I understood what the original >so I thought this was meant just to be an exercise in pattern >matching). However, using algebraic method, like the one above, >could be unnecessarily complicated if the second factor of the >equation is a high degree polynomial or something even more >complicated. It is in fact easier to use patterns. Compare: p[x_] = x^5*(x^32 + 2x^21 + 3x^17 - 13); >Timing[Cases[p[x], x^(n_.) -> n, {1}]] >{0.0005019999999973379*Second, {5}} specification is important). >Timing[Count[x /. Solve[p[x] == 0, x], 0]] >{0.08699899999999872*Second, 5} Andrzej Kozlowski Chiba, Japan >Hi Andrzej >>I have had occasion to use patterns before and by far I have found >>it the most difficult feature of mathematica to work with and the >>help on it leaves a lot to be desired. I agree the poster seems to >>be trying to learn patterns, I was just trying to point out, as you >>have on your earlier reply, that there are easier ways to do the >>same job (i.e finding the highest common exponent in x). Again not >>to put a finer point on it, it seems to me that the use of >>patterning as you have shown seems only to work when the expression >>is in its factored form (which I admit was the posters original >>query) whereas using Solve and count works irrespective of the >>form of the expression. Or am I wrong? > You are of course right. Mathematica's pattern matching is (with some > rudimentary exceptions) only syntactic, which means that expressions > are matched literally and not according to their mathematical > meaning (that would be semantic pattern matching). A factored > expression and an unfactored oen are quite different as far as > pattern matching is concerned. Algebraic methods can do much more > than pattern matching but they tend to be very slow and suitable > mostly for small problems. So both approaches have their strong > and weak points and one or the other should be preferred depending on > what one is trying to do. Obviously there is no panacea for all types > of situations, which is why posters should try to state exactly what > they are really trying to do. Andrzej Kozlowski >>p[x_] = x^5*(x^32 + 2x^21 + 3x^17 - 13x^5); >>Timing[Cases[p[x], x^(n_.) -> n, {1}]] >>{0. Second, {5}} >>Timing[Count[x /. Solve[p[x] == 0, x], 0]] >>{0.01 Second, 10} >>Now if you use simplify >>p[x_] = x^5*(x^32 + 2x^21 + 3x^17 - 13x^5) // Simplify; >>Timing[Cases[p[x], x^(n_.) -> n, {1}]] >>{0. Second, {10}} >>Pratik >>-- >>Pratik Desai >>Graduate Student >>UMBC >>Department of Mechanical Engineering >>Phone: 410 455 8134 A method that is reasonably fast but (for most purposes) more robust than syntactic approaches is to use Exponent with a third argument. In[3]:= Exponent[x^5*(x^32 + 2x^21 + 3x^17 - 13x^5), x, Min] Out[3]= 10 Since the topic was broached, there was some Mathematica work on semantic pattern matching a few years back. You might want to look at Jason Harris. Semantica: semantic pattern matching in Mathematica. The Mathematica Journal 7(3):329-360. http://library.wolfram.com/infocenter/Articles/3902/ Daniel Lichtblau Wolfram Research ==== Subject: Re: How to simulate random samples from crooked coin toss? >I want to simulate 100 tosses of a crooked coin where probability >of Heads is 0.6 and Tails is 0.4. The most straight forward way to do this would be to use the BinomialDistribution function in the Statistics`DiscreteDistributions` package. As follows: <0.4&/@Table[Random[],{100}], True] will accomplish the same thing -- To reply via email subtract one hundred and four ==== Subject: Re: Square-root Kalman Information filter The Kalman filter is mentioned in the Time Series package. Ref: http://www.wolfram.com/products/applications/timeseries/features.html ==== Subject: Re: How to simulate random samples from crooked coin toss? Here's my solution. There are probably much more elegant ways to do it, but this is quick: Get your probabilities (for the crooked coin, or die, or whatever) in the range [0, 1] for each side. That is, if the die has 6 sides, you could say probsOfEachSide={0.4, 0.15, 0.3, 0.1, 0.025, 0.025} so that side 1 has a 40% chance of coming up, side 2 has a 15% chance, etc. numberOfSide = probsOfEachSide//Length Note that probsOfEachSide//Total = 1. Create a list of cutoffs -- read the documentation about RangeCounts and RangeLists: cutoffs={0}; cutoffs[[1]] = probsOfEachSide//First; (AppendTo[cutoffs, probsOfEachSide[[#]] + cutoffs[[# - 1]]]) & /@ (Range[2, numberOfSides - 1]); Needs[Statistics`DataManipulation`] Here, we toss a coin 10000 (or however many you want) times: rawTosses = Random[] & /@ Range[10000]; If you want to see explicitly which tosses fall into which bins (between 0 and 0.4, or between 0.4 and 0.4+0.15, etc.) then you can use RangeLists[rawTosses, cutoffs] but I suspect that you want to use numberInEachBin=RangeCounts[rawTosses, cutoffs] which will actually count the number of tosses which fall into each bin. ListPlot[numberInEachBin, PlotJoined -> True] Alternatively, Histogram looks nice: Needs[Graphics`Graphics`] Histogram[numberInEachBin, FrequencyData -> True]; Hope that helps, C.O. >Math People, I'm new to Mathematica so please bear with me. I have a simple problem I want to solve and I'm looking >for some ideas to get started on it. I want to simulate 100 tosses of a crooked coin where >probability of Heads is 0.6 and Tails is 0.4. After I figure the coin out, I want to move on to crooked >dice and crooked card decks. So, really what I'm looking for has more to do with developing >a method to simulate the collection of random samples. I looked around in Help and found a promising sentence: Random[distribution] gives a random number with the specified >statistical distribution. Then I found a page here which has some clues: >http://mathworld.wolfram.com/DiscreteDistribution.html Unfortunately, I'm weak with math notation so the above >page makes little sense to me. So, let me put it to you as simply as I can: I want to define a function that could be called like this: numberOfHeads = coinTosser[infoAboutCoin, numberOfTosses, head] >numberOfTails = coinTosser[infoAboutCoin, numberOfTosses, tail] Once I figure out how to write the above function, >I could template off of it to build something like this: numberOfOnes = diceRoller[infoAboutThisDie, numberOfRolls, aOne] Do any of you have thoughts on how to approach the >writing of the above functions? -Dan > > -- PGP Key ID: 0x235FDED1 Please avoid sending me Word or PowerPoint attachments. http://www.gnu.org/philosophy/no-word-attachments.html ==== Subject: Re: How to simulate random samples from crooked coin toss? Hello Dan: Does TableForm[Table[ {i, If[Random[] > 0.6, H, T]}, {i, 100}]] get you started? MATTHIAS BODE ----- Original Message ----- ==== Subject: How to simulate random samples from crooked coin toss? > Math People, I'm new to Mathematica so please bear with me. I have a simple problem I want to solve and I'm looking > for some ideas to get started on it. I want to simulate 100 tosses of a crooked coin where > probability of Heads is 0.6 and Tails is 0.4. After I figure the coin out, I want to move on to crooked > dice and crooked card decks. So, really what I'm looking for has more to do with developing > a method to simulate the collection of random samples. I looked around in Help and found a promising sentence: Random[distribution] gives a random number with the specified > statistical distribution. Then I found a page here which has some clues: > http://mathworld.wolfram.com/DiscreteDistribution.html Unfortunately, I'm weak with math notation so the above > page makes little sense to me. So, let me put it to you as simply as I can: I want to define a function that could be called like this: numberOfHeads = coinTosser[infoAboutCoin, numberOfTosses, head] > numberOfTails = coinTosser[infoAboutCoin, numberOfTosses, tail] Once I figure out how to write the above function, > I could template off of it to build something like this: numberOfOnes = diceRoller[infoAboutThisDie, numberOfRolls, aOne] Do any of you have thoughts on how to approach the > writing of the above functions? -Dan > -- > dbikle@gmail.com ==== Subject: Re: How to simulate random samples from crooked coin toss? > Math People, I'm new to Mathematica so please bear with me. I have a simple problem I want to solve and I'm looking > for some ideas to get started on it. I want to simulate 100 tosses of a crooked coin where > probability of Heads is 0.6 and Tails is 0.4. After I figure the coin out, I want to move on to crooked > dice and crooked card decks. So, really what I'm looking for has more to do with developing > a method to simulate the collection of random samples. I looked around in Help and found a promising sentence: Random[distribution] gives a random number with the specified > statistical distribution. Then I found a page here which has some clues: > http://mathworld.wolfram.com/DiscreteDistribution.html Unfortunately, I'm weak with math notation so the above > page makes little sense to me. So, let me put it to you as simply as I can: I want to define a function that could be called like this: numberOfHeads = coinTosser[infoAboutCoin, numberOfTosses, head] > numberOfTails = coinTosser[infoAboutCoin, numberOfTosses, tail] Once I figure out how to write the above function, > I could template off of it to build something like this: numberOfOnes = diceRoller[infoAboutThisDie, numberOfRolls, aOne] Do any of you have thoughts on how to approach the > writing of the above functions? To simulate a crooked coin use the Bernoulli distribution: < numberOfHeads = coinTosser[infoAboutCoin, numberOfTosses, head] the problem can be solved without simulations For your purpose, to describe a coin (dice, cards) you have to specify the propability that a certain event happens, e.g. Head, call it Ph. A fine coin would have Ph=0.5, a croocked coin e.g. Ph=0.6. Now as your coin tosses are all independent, the mean number of Heads in n tosses will simply be: n Ph sincerely, Daniel > Math People, I'm new to Mathematica so please bear with me. I have a simple problem I want to solve and I'm looking > for some ideas to get started on it. I want to simulate 100 tosses of a crooked coin where > probability of Heads is 0.6 and Tails is 0.4. After I figure the coin out, I want to move on to crooked > dice and crooked card decks. So, really what I'm looking for has more to do with developing > a method to simulate the collection of random samples. I looked around in Help and found a promising sentence: Random[distribution] gives a random number with the specified > statistical distribution. Then I found a page here which has some clues: > http://mathworld.wolfram.com/DiscreteDistribution.html Unfortunately, I'm weak with math notation so the above > page makes little sense to me. So, let me put it to you as simply as I can: I want to define a function that could be called like this: numberOfHeads = coinTosser[infoAboutCoin, numberOfTosses, head] > numberOfTails = coinTosser[infoAboutCoin, numberOfTosses, tail] Once I figure out how to write the above function, > I could template off of it to build something like this: numberOfOnes = diceRoller[infoAboutThisDie, numberOfRolls, aOne] Do any of you have thoughts on how to approach the > writing of the above functions? -Dan ==== Subject: Wrong Integral result for a Piecewise function This integral gives gives the wrong result for the interval [2,3] h[x_] := Integrate[Boole[x - 1 < 2 y + 2 z < x], {y, 0, 1}, {z, 0, 1}] Plot[h[x],{x,0,5}] and Plot[Evaluate[h[x]],{x,0,5}] give different plots, The curve should be smooth and bell shaped from 0 to 5, so the first looks correct. The second has a jump discontinuity at 2 and 3. Also h[5/2] and h[x]/.x-> 5/2 give different answers This is using some new features in Mathematica 5.1. Same result on a Mac (10.4) and SunOS (5.9). Breaking into a difference of two integrals gives the correct answer: Integrate[Boole[ 2 y + 2 z < x], {y, 0, 1}, {z, 0, 1}] - Integrate[Boole[ 2 y + 2 z < x-1], {y, 0, 1}, {z, 0, 1}] Finally Integrate[h[x], {x, 0, 5}] and NIntegrate[h[x], {x, 0, 5}] both give the wrong answer, it should be 1. The triple integral is correct Integrate[Boole[x - 1 < 2 y + 2z < x], {y, 0, 1}, {z, 0, 1}, {x, 0, 5}] Any suggestions on integrating over regions with linear constraints? Versions 5.1 has powerful new piecewise functions ==== Subject: Re: Partial diff equations > I have a not difficult to integrate but huge system of partial > differential equations that I would never attempt to solve by hand. So I > tried to feed it to mathematica and got the message bellow. I got annoyed and > tested DSolve with a trivial problem only to realize that, apparently, > mathematica is not very good when it comes to partial diff equations. Indeed, how come mathematica can't solve this simple system: DSolve[{D[f[x,y],x]==2 x y^2, D[f[x,y],y]==2 x^2 y}, f[x,y], {x, y}] the solution is trivial (f[x,y]=x^2 y^2), but if I enter the above > command I get: DSolve::overdet: > The system has fewer dependent variables than equations, so is > overdetermined. any info would be appreciated, > David Boily > Centre for Intelligent Machines > McGill University > Montreal, Quebec > Hello David, At present, DSolve can handle general first order partial differential equations (they can be linear, quasilinear or nonlinear) and a limited class of second order partial differential equations. A fairly detailed description of this functionality is available in the Advanced Documentatation for DSolve in Mathematica 5.1.1. There is no support for systems of partial differential equations such as the one given by you, although this is planned for a future release. One issue with solving a system of this type is that it is overdetermined (that is, there are more equations than unknowns) and hence there may be no solution. The DSolve::overdet message in earlier versions indicates this difficulty. It is not generated in Mathematica 5.1 because the message is confusing in situations where a solution does exist. Here are two suggestions for solving the system sent by you. We begin by setting up the system. In[1]:= $Version Out[1]= 5.1 for Linux (February 20, 2005) In[2]:= sys = {D[f[x, y], x] == 2*x*y^2, D[f[x, y], y] == 2*x^2*y}; Next, we solve the first member of the system, which is a single first order linear partial differential equation, using DSolve. In[3]:= sol1 = DSolve[sys[[1]], f, {x, y}, GeneratedParameters -> g] 2 2 Out[3]= {{f -> Function[{x, y}, x y + g[1][y]]}} Here, g[1][y] is an arbitrary function of y. It can be determined by using the second member of the system to arrive at the solution of the system. In[4]:= sol2 = DSolve[(D[f[x, y], y] /. sol1[[1]]) == 2 x^2 y, g[1], {y}] Out[4]= {{g[1] -> Function[{y}, C[1]]}} In[5]:= f[x, y] /. sol1[[1]] /. sol2[[1]] 2 2 Out[5]= x y + C[1] A slightly different approach is to look for polynomial solutions of the system as follows. In[1]:= f[x_, y_] = Sum[c[i, j]*x^i*y^j, {i, 0, 3}, {j, 0, 3}]; In[2]:= InputForm[Solve[(#1 == 0 & ) /@ Flatten[CoefficientList[{D[f[x, y], x] - 2*x*y^2, D[f[x, y], y] - 2*x^2*y}, {x, y}]], Flatten[Table[c[i, j], {i, 0, 3}, {j, 0, 3}]]]] Solve::svars: Equations may not give solutions for all solve variables. Out[2]//InputForm= {{c[0, 1] -> 0, c[0, 2] -> 0, c[0, 3] -> 0, c[1, 0] -> 0, c[1, 1] -> 0, c[1, 2] -> 0, c[1, 3] -> 0, c[2, 0] -> 0, c[2, 1] -> 0, c[2, 2] -> 1, c[2, 3] -> 0, c[3, 0] -> 0, c[3, 1] -> 0, c[3, 2] -> 0, c[3, 3] -> 0}} In[3]:= f[x, y] /. %[[1]]/. c[0,0] -> c 2 2 Out[3]= c + x y Sorry for the inconvenience caused by this limitation. Devendra Kapadia, Wolfram Research, Inc. ==== Subject: Re: Partial diff equations You are expecting too much. At the moment (perhaps things will be different in 2100) computer algebra systems can help humans do mathematics as tools, but cannot carry out any deep developments on their own. Its the difference between (a+b)^2=a^2+2ab+b^2 and FLT. PDEs is one area where only humans can go deep because there are no general solution methods, only some generic rules (Greens functions, domain of validity, etc). Few can be solved in closed form for arbitrary boundary or initial conditions. I noticed that your example has no specified domain or BCs. It is a particular solution. On the other hand, many ODE classes are amenable to general solution procedures, e.g. using Laplace or Fourier transforms, so they can be put in a rule database, just like integrals. ==== Subject: Re: Partial diff equations >I have a not difficult to integrate but huge system of partial >differential equations that I would never attempt to solve by hand. So I >tried to feed it to mathematica and got the message bellow. I got annoyed and >tested DSolve with a trivial problem only to realize that, apparently, >mathematica is not very good when it comes to partial diff equations. Indeed, how come mathematica can't solve this simple system: DSolve[{D[f[x,y],x]==2 x y^2, D[f[x,y],y]==2 x^2 y}, f[x,y], {x, y}] the solution is trivial (f[x,y]=x^2 y^2), but if I enter the above >command I get: DSolve::overdet: > The system has fewer dependent variables than equations, so is > overdetermined. any info would be appreciated, >David Boily >Centre for Intelligent Machines >McGill University >Montreal, Quebec > I am not sure exactly what system you refer to in your expression, but if you try this sol1 = DSolve[D[f[x, y], x] == 2 x y^2, f[x, y], {x, y}] // First; sol2 = DSolve[D[f[x, y], y] == 2 x^2 y, f[x, y], {x, y}] // First; f1[x_, y_] = f[x, y] /. sol1 f2[x_, y_] = f[x, y] /. sol2 >>!(x^2 y^2 + (C[1])[y]) >>!(x^2 y^2 + (C[1])[x]) D[f1[x, y], x] D[f2[x, y], y] >>2xy^2 >>2x^2y It seems to work fine Pratik -- Pratik Desai Graduate Student UMBC Department of Mechanical Engineering Phone: 410 455 8134 ==== Subject: Simplify and FullSimplify A result of a calculation I was doing generated this expression.... q-q Exp[-a x] + c Exp[-a x] naturally my next step was Simplify and I thought I'd get the Exp[- ax] collected.....to my complete surprize I got the following: Exp[-a x] (c + (-1+ Exp[a x]) q How on Earth did Mathematica come up with this? I checked FullSimplify which did collect Exp[-a x].... On re-reading my question before I submitted it, I see that with Simplify Mathematica 'collected' using Exp[- a x] q.....of course, visually this expression seems quite complex and would seem to take much more 'thinking' to get ......why do Simplify and FullSimplify have such a vast difference in what is considered 'Simpler'? ==== Subject: Re: Function to handle array with variable _number_ of dimensions? Hi Joe, English speaking people often complain about German speaking people making long contorted, hard to understand, sentences. But consider: its value times the sum over consecutive diamond-shaped shells of the averages of the other values in that shell, weighted by the reciprocal of their taxicab distance from the middle cell. maybe you would enjoy learning German. Well, now your question. If what you call quantity is a linear function of the array elements, then the thing you are looking for is called convolution. It is heavily used in 1 and 2 dimensions for signal and image processing. I never saw it in higher dimension, but it is stright forward. In Mathematica there is ListConvolve for this purpose. Let's assume we want to lightly smear out a signal by replacing every point by the avarage of its neighbours. 1 dimensional example: This would e.g. require a kernel of {1,1,1}/3 d=Table[0,{5}]; d[[3]]=1; ker={1,1,1}/3; ListConvolve[ker,d,{3}] this would transform a point: into a smeared out point: {0, 1/3, 1/3, 1/3, 0} 2 dimensional example: Kernel: {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}} d = Table[0, {5}, {5}]; d[[3, 3]] = 1; ker = Table[1, {3}, {3}]/9; ListConvolve[ker, d, {2, 2}] this would e.g. transform a point 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 into a smeared out point: 0 0 0 0 0 0 1/9 1/9 1/9 0 0 1/9 1/9 1/9 0 0 1/9 1/9 1/9 0 0 0 0 0 0 3 dimensional example: d = Table[0, {5}, {5},{5}]; d[[3, 3,3]] = 1; ker = Table[1, {3}, {3}, {3}]/27; ListConvolve[ker, d, {2, 2,2}] The output is a smeared out cube.. sincerely, Daniel > >Hi Joe, >>if you want a descent answer, please try to phrase your question that it >>becomes sensible. >>You have 2 integer arguments, P and Q. Why is PxPx..xP an array? What >>does x mean? Is P an array? >>... >> > > Sorry to be unclear. P is an Integer and x means by in my original >post (at the end of this post), so PxPx...xP is not an array itself, but >rather describes the size and shape of an array. I want to compute various quantities derived from a Q-dimensional array >of P^Q integers, where not only the individual integers, but also their >position within the array matters. > E.G. >In the case P=7 and Q=2 a typical array of 49 integers is: >2 45 32 31 28 22 43 39 3 40 30 5 36 10 29 6 16 27 15 34 12 4 7 8 21 26 19 46 35 20 24 42 14 23 37 9 25 48 1 33 41 17 38 18 0 44 11 13 47 >and a typical quantity would be the average, over the entire array, of >the function which assigns to, for example, the middle cell the value >21*[(26+27+8+42)/4 + (19/2+15/2+30/2+16/2+7/2+24/2+1/2+14/2)/8 + >(46/3+34/3+...+23/3)/12 + ...], i.e. its value times the sum over >consecutive diamond-shaped shells of the averages of the other values in >that shell, weighted by the reciprocal of their taxicab distance from >the middle cell. Or, in the case P=3 and Q=4, the array (of 81 integers) might be: >4 52 18 68 69 77 46 8 30 >79 32 20 76 47 24 23 73 13 >66 62 55 67 5 0 75 34 49 33 63 53 7 36 64 72 39 60 >48 6 70 29 28 37 12 44 58 >74 35 9 45 27 40 22 26 15 61 59 16 51 57 2 31 14 50 >54 71 10 17 11 42 19 21 56 >65 38 41 3 43 80 78 25 1 >and the quantity might be the sum of the values of a function on the >array, which, at the cell with coordinates (1,2,3,2), depends on the the >numbers 12 (at that cell) and 19, 22, 44, 72, 23, and 29 in the >immediately adjacent cells, for instance >(12-19)+(12-22)+(12-44)+(12-72)+(12-23)+(12-29). The point is that once I am given P and Q, I know how to compute the >P^Q values in the array, but what I want to do is calculate a sort of >integral over the entire array of the values smeared by a sort of >kernel function of the Q coordinates. In other words, I'd like to write a function like: >F[P_Integer,Q_Integer]:= >Sum[ > g[P,Q,z1,z2, ... ,zQ], > {z1,0,P},{z2,0,P}, ... ,{zQ,0,P} >] >where I have already defined >g[P_Integer,Q_Integer,zList__Integer]:= >SomeExpression/;Length[zList]==Q >My problem is that I don't know how, in Mathematica, to handle the >ellipsis in order to allow for the variable number, Q, of indices for >the array. > >I'd like to write a function that takes two integer arguments, P and Q >say, and then returns a value calculated from the P x P x ... x P (Q >factors) array, whose entries depend on both P and Q. My first impulse >is to try and iterate over the array, but I don't see how to generate a > non-fixed number of iteration variables. Does anyone have a suggestion of a good way to do this, short of writing >a a distinct variant function for each possible value of Q? > > -- Daniel Huber Metrohm Ltd. Oberdorfstr. 68 CH-9100 Herisau Tel. +41 71 353 8585, Fax +41 71 353 8907 E-Mail: Internet: ==== Subject: Re: Function to handle array with variable _number_ of dimensions? Let h[k,m,{p,q}] denote the element km of the array, h[k,m,n,{p,q}] denote element kmn of the array and so forth, where the sublist simply allows the array element to depend on p and q. Let g denote the function you wish to apply to your array. In[37]:= fun[p_,q_]:= g[Table[h[Sequence@@Array[i,q],{p,q}], Evaluate[Sequence@@Table[{i[j],p},{j,q}]]]]; For example, setting p=2 and q=2 yields a 2x2 array to which g is applied: In[39]:= fun[2,2] Out[39]= g[{{h[1,1,{2,2}],h[1,2,{2,2}]},{h[2,1,{2,2}],h[2,2,{2,2}]}}] Setting p=2 and q=3 yields a 2x2x2 array (i.e., p x p x p (i.e., q factors)) to which g is applied: In[40]:= fun[2,3] Out[40]= g[{{{h[1,1,1,{2,3}],h[1,1,2,{2,3}]},{h[1,2,1,{2,3}], h[1,2,2,{2,3}]}},{{h[2,1,1,{2,3}],h[2,1,2,{2,3}]},{h[2,2,1,{2,3}], h[2,2,2,{2,3}]}}}] ==== Subject: Re: Function to handle array with variable _number_ of dimensions? > Hi Joe, > if you want a descent answer, please try to phrase your question that it > becomes sensible. > You have 2 integer arguments, P and Q. Why is PxPx..xP an array? What > does x mean? Is P an array? > ... Sorry to be unclear. P is an Integer and x means by in my original post (at the end of this post), so PxPx...xP is not an array itself, but rather describes the size and shape of an array. I want to compute various quantities derived from a Q-dimensional array of P^Q integers, where not only the individual integers, but also their position within the array matters. E.G. In the case P=7 and Q=2 a typical array of 49 integers is: 2 45 32 31 28 22 43 39 3 40 30 5 36 10 29 6 16 27 15 34 12 4 7 8 21 26 19 46 35 20 24 42 14 23 37 9 25 48 1 33 41 17 38 18 0 44 11 13 47 and a typical quantity would be the average, over the entire array, of the function which assigns to, for example, the middle cell the value 21*[(26+27+8+42)/4 + (19/2+15/2+30/2+16/2+7/2+24/2+1/2+14/2)/8 + (46/3+34/3+...+23/3)/12 + ...], i.e. its value times the sum over consecutive diamond-shaped shells of the averages of the other values in that shell, weighted by the reciprocal of their taxicab distance from the middle cell. Or, in the case P=3 and Q=4, the array (of 81 integers) might be: 4 52 18 68 69 77 46 8 30 79 32 20 76 47 24 23 73 13 66 62 55 67 5 0 75 34 49 33 63 53 7 36 64 72 39 60 48 6 70 29 28 37 12 44 58 74 35 9 45 27 40 22 26 15 61 59 16 51 57 2 31 14 50 54 71 10 17 11 42 19 21 56 65 38 41 3 43 80 78 25 1 and the quantity might be the sum of the values of a function on the array, which, at the cell with coordinates (1,2,3,2), depends on the the numbers 12 (at that cell) and 19, 22, 44, 72, 23, and 29 in the immediately adjacent cells, for instance (12-19)+(12-22)+(12-44)+(12-72)+(12-23)+(12-29). The point is that once I am given P and Q, I know how to compute the P^Q values in the array, but what I want to do is calculate a sort of integral over the entire array of the values smeared by a sort of kernel function of the Q coordinates. In other words, I'd like to write a function like: F[P_Integer,Q_Integer]:= Sum[ g[P,Q,z1,z2, ... ,zQ], {z1,0,P},{z2,0,P}, ... ,{zQ,0,P} ] where I have already defined g[P_Integer,Q_Integer,zList__Integer]:= SomeExpression/;Length[zList]==Q My problem is that I don't know how, in Mathematica, to handle the ellipsis in order to allow for the variable number, Q, of indices for the array. > I'd like to write a function that takes two integer arguments, P and Q >> say, and then returns a value calculated from the P x P x ... x P (Q >> factors) array, whose entries depend on both P and Q. My first impulse >> is to try and iterate over the array, but I don't see how to generate a >> non-fixed number of iteration variables. >> Does anyone have a suggestion of a good way to do this, short of writing >> a a distinct variant function for each possible value of Q? > -- ------------------ http://public.xdi.org/=joe.christy ------------------ == If I can save you any time, give it to me, I'll keep it with mine. == ==== Subject: Re: Re: Function to handle array with variable _number_ of dimensions? >>I'd like to write a function that takes two integer arguments, P and Q >>say, and then returns a value calculated from the P x P x ... x P (Q >>factors) array, whose entries depend on both P and Q. My first impulse >>is to try and iterate over the array, but I don't see how to generate a >> non-fixed number of iteration variables. >>Does anyone have a suggestion of a good way to do this, short of writing >>a a distinct variant function for each possible value of Q? > I'm not exactly sure what you are trying to do, but I would have a look > at Outer and Tuples (in 5.1). Also have a look at the Advanced > Documentation for these functions in the Help Browser. Paul > These are probably the best approaches. If explicit iteration is for some reason really required, then you can generate the indexing programmatically as below. arrayIndices[pp_,qq_] := Module[ {indices=Array[index,qq]}, Apply[Sequence,Map[{#,pp}&, indices]] ] Example: In[2]:= arrayIndices[3,4] Out[2]= Sequence[{index[1], 3}, {index[2], 3}, {index[3], 3}, {index[4], 3}] It is not hard to modify to have later index bounds depend on earlier ones, etc. But if your index requirements are tensorial that is, no such dependencies), then the functionality offered by Outer and relatives is almost certainly going to be better for your purposes than generating and using an explicit set as above. Daniel Lichtblau Wolfram Research ==== Subject: Re: Function to handle array with variable _number_ of dimensions? > I'd like to write a function that takes two integer arguments, P and Q > say, and then returns a value calculated from the P x P x ... x P (Q > factors) array, whose entries depend on both P and Q. My first impulse > is to try and iterate over the array, but I don't see how to > generate a > non-fixed number of iteration variables. Does anyone have a suggestion of a good way to do this, short of > writing > a a distinct variant function for each possible value of Q? Your question is unclear, do you have a Q dimensional array that you are extracting elements from, or a 1 dimensional array that you are extracting the P^Qth element from? Incidentally you probably should use lower case variables. If it's the first case just use Table arr[[Sequence@@Table[p,{q}]]] will extract the element p,p,...p repeated q times of arr. Ssezi ==== Subject: Re: Function to handle array with variable _number_ of dimensions? Hi Joe, if you want a descent answer, please try to phrase your question that it becomes sensible. You have 2 integer arguments, P and Q. Why is PxPx..xP an array? What does x mean? Is P an array? sincerely, Daniel > I'd like to write a function that takes two integer arguments, P and Q > say, and then returns a value calculated from the P x P x ... x P (Q > factors) array, whose entries depend on both P and Q. My first impulse > is to try and iterate over the array, but I don't see how to generate a > non-fixed number of iteration variables. Does anyone have a suggestion of a good way to do this, short of writing > a a distinct variant function for each possible value of Q? >