mm-705
===
Subject: Re: Re: Zero or one
> The following definitions are not equivalent:
> In[1]:=
> optional1[expr_] := expr | ___ ? (Length[{#}] == 0&);
> optional2[expr_]:=expr|___?(#==Null&);
> In[3]:=
> MatchQ[{1,,3},{1,optional1[2],3}]
> Out[3]=
> False
> In[4]:=
> MatchQ[{1,,3},{1,optional2[2],3}]
> Out[4]=
> True
Yes, I never considered the possibility of a List with missing
entries (explicit Null). The reason why the definitions are not
equivalent is that Null vanishes only when it is the final value
returned by a computation, so
Length[{Null}]
1
Of course it is not entirely obvious which interpretation of 0 or 1
is more correct in the case of something like {1, ,3}. Would you say
that the second entry (nothing) is correctly described as 0 or 1
of something? Or is Null, a positive nothingness and quite
different from the simple absence of anything? Actually, in
Mathematica it is sometime one and sometime the other. But just in
case anybody takes this too seriously I believe philosophical
considerations of the nature of nothingness are best left to writers
like Stanislaw Lem ;-)
Andrzej Kozlowski
===
Subject: Re: A list of numbers without 73
Clear[S,S2];
S[1]=Range[1, 9];
S[i_?Positive]:=S[i]=
Delete[Union[Table[
k*(1-Boole[StringMatchQ[ToString[k], *73*]]),
{k, 10^(i-1), 10^i-1}]], 1];
S2[1]=Range[1, 9];
S2[i_?Positive]:=S2[i]=
DeleteCases[Range[10^(i-1), 10^i-1],
_?(StringMatchQ[ToString[#], *73*]&)]
And@@Table[S[i]==S2[i],{i, 4}]
True
Timing[S[6]][[1]]
36.1983 Second
Timing[S2[6]][[1]]
28.961 Second
%/%%
0.800065
S[6]==S2[6]
True
Bob Hanlon
===
> Subject: A list of numbers without 73
> Experts,
> S(i) should be a list of positive integers without the substring 73.
> My code seems to be very inefficient. I multiply all numbers with 0 if
they
> contain 73, sort the new list, make the union (remove double elements)
and
> delete the first element. Therefore it is necessary to use an if statement
> in order not to delete the first element of the numbers 1-9. Any more
> sophisticated ideas?
> L := Table[k, {k, 1, 9}]
> S[i_] := S[i] =
> If[i == 1, L,
> Delete[Union[
> Table[k*(1 - Boole[StringMatchQ[ToString[k], *73*]]), {k,
> 10^(i - 1), 10^i - 1}]], 1]]
> Best
> Thomas
===
Subject: Re: Roman Maeder's Classes.m
>> Or we could ask Andrzej to explain briefly his comments: slightest
>> liking for
>> this [Maeder] approach and I prefer the Combinatorica package by far.
>> This implies that oop is implemented in a superior way in
>> Combinatorica?
> No, that means I do not much like the whole idea of using OOP in
> Mathematica. I guess the main reason is that I find this style of
> programming so alien to the way I usually program in Mathematica
> that I can never remember how anything works and always have to read
> the documentation from scratch. Of course I do not have such problems
> with the Combinatorica package, which is why I have always been using
> it in teaching Graph Theory and never was tempted to use Gray's
> approach based on Maeder's classes.
> Andrzej Kozlowski
A better implementation can provide an extremely intuitive and easy to use
interface. I agree that Maeder's Classes are rather cumbersome to work
with. It's very difficult to determine how useful a builtin, or better
implemented/integrated OOP package might be.
--
The Mathematica Wiki: http://www.mathematica-users.org/
Math for Comp Sci http://www.ifi.unizh.ch/math/bmwcs/master.html
Math for the WWW: http://www.w3.org/Math/
===
Subject: Re: functional programming
Do works fine in this case
Clear[dioph1,dioph2];
dioph1[x_,k_]:=
(5 x)^2-2^k*3*(5+k)^2-131*k-7;
dioph2[x_,k_]:=
If[(5 x)^2-2^k*3*(5+k)^2-131*k-7==0,
Sow[{x,k}]];
n=400;
Reap[Do[If[dioph1[x,k]==0,Sow[{x,k}]],
{x,0,n},{k,0,n}]][[-1,1]]//Timing
{7.1011679999999995*Second, {{31, 6}}}
Reap[Do[dioph2[x,k],
{x,0,n},{k,0,n}]][[-1,1]]//Timing
{7.551326000000001*Second, {{31, 6}}}
Reap[Table[dioph2[x,k],
{x,0,n},{k,0,n}]][[-1,1]]//Timing
{7.670505999999996*Second, {{31, 6}}}
Bob Hanlon
===
> Subject: functional programming
> Hi All
> As a long time C++ programmer I am terrible at functional programming.
> I recently replied to a post on here concerning diophantine equations
> and provided the following code:
> dioph[x_, k_] := (5 x)^2 - 2^k*3*(5 + k)^2 - 131*k - 7
> Reap[
> Do[
> Do[
> If[dioph[x, k] == 0, Sow[{x, k}];];
> , {x, 0, 1000}
> ]
> , {k, 0, 1000}
> ]]
> Someone else came up with a more efficient way of doing this and it was
> a nice solution. However my question is this - How could I have
> rewritten my orginal code using Functional programming? Nothing fancy
> - just a literal translation using the ideas of functional programming.
> I tried
> dioph[x_, k_] := If[(5 x)^2 - 2^k*3*(5 + k)^2 - 131*k - 7 == 0, {x,
> k}];
> Table[dioph[x, k], {x, 0, 10}, {k, 0, 10}];
> which is similar to something that was posted on here a while back but
> I hit the same problem they did - The If statement returns Null when
> the statement is false so you end up with large lists of Nulls. Not
> good if I want to look at large ranges of numbers.
> Any advice would be welcomed
> Mike
===
Subject: Re: functional programming
>As a long time C++ programmer I am terrible at functional
>programming. I recently replied to a post on here concerning
>diophantine equations and provided the following code:
>dioph[x_, k_] := (5 x)^2 - 2^k*3*(5 + k)^2 - 131*k - 7
>Reap[
> Do[
> Do[
> If[dioph[x, k] == 0, Sow[{x, k}];];
> , {x, 0, 1000}
> ]
> , {k, 0, 1000}
> ]]
>How could I have rewritten my orginal code using Functional
>programming?
>I tried
>dioph[x_, k_] := If[(5 x)^2 - 2^k*3*(5 + k)^2 - 131*k - 7 == 0, {x,
>k}]; Table[dioph[x, k], {x, 0, 10}, {k, 0, 10}];
Here is one approach
Cases[
Flatten[
Table[{x, k, (5*x)^2 - 2^k*3*(5 + k)^2 - 131*k - 7}, {x, 50},
{k, 50}],
1],
{__, 0}]
{{31, 6, 0}}
Another approach would be
dioph[x_, k_] := (5*x)^2 - 2^k*3*(5 + k)^2 - 131*k - 7
Cases[
Flatten[
Outer[{#1, #2, dioph[#1, #2]}&, Range[50], Range[50]],
1],
{__, 0}]
{{31, 6, 0}}
--
To reply via email subtract one hundred and four
===
Subject: Re: Skewness problem
>please see
>http://www.wiwi.uni-muenster.de/statistik/download/vorlesungen/
>empwifo/kapitel_2.pdf See fig. 5 on page 18: When you compare the
>histogram in the middle and the lower histogram it looks like we
>have the same sample data, arranged in two different ways, from
>left to right and vice versa. The skewnesses are +0.866 and -0.895.
>I am confused.
How do you conclude these are two histograms of the same sample data?
The middle histogram shows over 500 observations with value 0.1. The lower
histogram shows no observations with value 0.1. These histograms are clearly
for dfferent data sets.
--
To reply via email subtract one hundred and four
===
Subject: Re: Assign a value to a variable
>I have a list of variables: vars = {a, b, c, d, e, f, g, h, i}
>and a list of numbers: numbers = Range[9]
>Now i'd like to choose randomly a variable and a number. and then
>i'd like to assign the number to the variable. I thought of
>somehting like that:
>vars[[Random[Integer, {1, Length[vars]}]]] =
>numbers[[Random[Integer, {1, Length[numbers]}]]]
>but it doesn't work.
In[52]:=
Set[#,numbers[[Random[Integer,{1,9}]]]]&/@vars;
vars
Out[53]=
{2,2,9,1,7,6,5,1,5}
Note, this will not work when a, b, c ... have already been assigned values.
Nor does it prevent two of your variables from being assigned the same
value.
--
To reply via email subtract one hundred and four
===
Subject: Re: A list of numbers without 73
>S(i) should be a list of positive integers without the substring
>73.
>My code seems to be very inefficient. I multiply all numbers with 0
>if they contain 73, sort the new list, make the union (remove
>double elements) and delete the first element. Therefore it is
>necessary to use an if statement in order not to delete the first
>element of the numbers 1-9. Any more sophisticated ideas?
>L := Table[k, {k, 1, 9}]
>Delete[Union[
>Table[k*(1 - Boole[StringMatchQ[ToString[k], *73*]]), {k,
>10^(i - 1), 10^i - 1}]], 1]]
It appears you want to generate a list of positive integers between 1 and
10^n - 1 with all integers having the sequence 73 being deleted. If so, a
simpler approach would be
s[n_]:=
Select[Range[10^n-1], !StringMatchQ[ToString@#, *73*]&]
checking that this works as intended
In[55]:= Complement[Range[999], s[3]]
Out[55]=
{73, 173, 273, 373, 473, 573,
673, 730, 731, 732, 733,
734, 735, 736, 737, 738,
739, 773, 873, 973}
--
To reply via email subtract one hundred and four
===
Subject: Re: general nth term of series
a small correction to my last posting:
RSolve[...a[2]==-1...], of course - but leads only to another useless
result.
===
Subject: Re: general nth term of series
> Hi Ash,
> SeriesTerm[(1 - 2*x*u + u^2)^(-1/2), {u, 0, n}]
> gives LegendreP[n,x] without any Gammas in Version 5.1
> Use SeriesTerm with care. It is quite buggy for general n:
> Series[Sin[x]/(1 + x), {x, 0, 5}]//Normal
> --> x - x^2 + (5*x^3)/6 - (5*x^4)/6 + (101*x^5)/120
> SeriesTerm[Sin[x]/(1 + x), {x, 0, 5}]
> --> 101/120
> is OK, but:
> SeriesTerm[Sin[x]/(1 + x), {x, 0, n}] /. n -> 5
> --> Sqrt[Pi/2]*BesselJ[1/2, 1]
> N[120 %]
> --> 100.977
> _Incidentally_ almost good...
> SeriesTerm gives for this example (-(-1)^n)*Sqrt[Pi/2]*BesselJ[1/2, 1]
> as coefficient of x^n. :-
> Peter
I should have pointed out that Sqrt[Pi/2]BesselJ[1/2,1]==Sin[1] which is
not too far from the coefficient of x^(2k+1) in
f[x]=(1 - x)Sum[Round[Sin[1](2k + 1)!]/(2k + 1)!*x^(2k + 1),{k, 0,
.89Ãõ}].
I have no idea how Mathematica should derive the representation
f[x]=Sum[(-1)^(k)*Sum[(-1)^j/(2*j + 1)!, {j, 0, Floor[k/2]}]x^(k + 1),
{k, 0, .89Ãõ}], because the result of
RSolve[{a[0]==0,a[1]==1,a[2]==0,(k+1)(k+2)(a[k+1]+a[k+2])+a[k]+a[k-1]==0},a,
k]
is spectacular ;-) but doesn't really help :-(.
Peter
===
Subject: Re: functional programming
> Hi All
> As a long time C++ programmer I am terrible at functional programming.
> I recently replied to a post on here concerning diophantine equations
> and provided the following code:
> dioph[x_, k_] := (5 x)^2 - 2^k*3*(5 + k)^2 - 131*k - 7
> Reap[
> Do[
> Do[
> If[dioph[x, k] == 0, Sow[{x, k}];];
> , {x, 0, 1000}
> ]
> , {k, 0, 1000}
> ]]
> Someone else came up with a more efficient way of doing this and it was
> a nice solution. However my question is this - How could I have
> rewritten my orginal code using Functional programming? Nothing fancy
> - just a literal translation using the ideas of functional programming.
> I tried
> dioph[x_, k_] := If[(5 x)^2 - 2^k*3*(5 + k)^2 - 131*k - 7 == 0, {x,
> k}];
> Table[dioph[x, k], {x, 0, 10}, {k, 0, 10}];
> which is similar to something that was posted on here a while back but
> I hit the same problem they did - The If statement returns Null when
> the statement is false so you end up with large lists of Nulls. Not
> good if I want to look at large ranges of numbers.
> Any advice would be welcomed
> Mike
Hi Mike,
one possibility is, to do some postprocessing:
tb = Table[dioph[x, k], {x, 30, 40}, {k, 0, 10}];
Flatten[tb, 1] /. Null -> Sequence[]
--> {{31, 6}}
Peter
===
Subject: Re: functional programming
Hi Mike,
You do not need to make a table if you only want a few elements.
E.g.: instead of:
Table[dioph[x, k], {x, 0, 10}, {k, 0, 10}];
try:
res={};
Do[If[(t=dioph[x, k])!=Null,AppendTo[res,t], {x, 0, 10}, {k, 0, 10}];
Daniel
> Hi All
> As a long time C++ programmer I am terrible at functional programming.
> I recently replied to a post on here concerning diophantine equations
> and provided the following code:
> dioph[x_, k_] := (5 x)^2 - 2^k*3*(5 + k)^2 - 131*k - 7
> Reap[
> Do[
> Do[
> If[dioph[x, k] == 0, Sow[{x, k}];];
> , {x, 0, 1000}
> ]
> , {k, 0, 1000}
> ]]
> Someone else came up with a more efficient way of doing this and it was
> a nice solution. However my question is this - How could I have
> rewritten my orginal code using Functional programming? Nothing fancy
> - just a literal translation using the ideas of functional programming.
> I tried
> dioph[x_, k_] := If[(5 x)^2 - 2^k*3*(5 + k)^2 - 131*k - 7 == 0, {x,
> k}];
> Table[dioph[x, k], {x, 0, 10}, {k, 0, 10}];
> which is similar to something that was posted on here a while back but
> I hit the same problem they did - The If statement returns Null when
> the statement is false so you end up with large lists of Nulls. Not
> good if I want to look at large ranges of numbers.
> Any advice would be welcomed
> Mike
===
Subject: Re: Assign a value to a variable
Ben,
The following function appeared on the news group some time ago that I
find useful for randomly shuffling a list (unfortunately I cannot
recall the name of the author):
shuffle [lst1_] :=
Module[{ newj, lst = lst1, n},
n = Length[lst];
Do[
newj = Random[Integer, {j, n}];
lst[[{j, newj}]] = lst[[{newj, j}]],
{j, n - 1}]; lst ]
Then it is a simple matter to do the assignment:
vars={a,b,c,d,e,f,g,h,i};
Evaluate[vars] = shuffle[Range[9]]
{2, 9, 6, 4, 7, 5, 1, 3, 8}
Brian
> Hi!
> I have a list of variables:
> vars = {a, b, c, d, e, f, g, h, i}
> and a list of numbers:
> numbers = Range[9]
> Now i'd like to choose randomly a variable and a number. and then i'd
> like to assign the number to the variable.
> I thought of somehting like that:
> vars[[Random[Integer, {1, Length[vars]}]]] =
> numbers[[Random[Integer, {1, Length[numbers]}]]]
> but it doesn't work.
> i hope you have an idea!
> thank you for answering!
> MFG Ben
===
Subject: Re: Assign a value to a variable
I think your code does work, however, it only assigns one value to one
variable. You would need to run through the entire list of variables to
get what you want. Note that it is not necessary to shuffle both left
and right hand side of the equals side. Here's a simple one-liner that
does the trick also:
Evaluate[Sort[vars, If[Random[] < 1/2, False] &]] = numbers;
The Sort function with the Random[] inside takes care of the shuffling.
Tammo Jan Dijkema
> Hi!
> I have a list of variables:
> vars = {a, b, c, d, e, f, g, h, i}
> and a list of numbers:
> numbers = Range[9]
> Now i'd like to choose randomly a variable and a number. and then i'd
> like to assign the number to the variable.
> I thought of somehting like that:
> vars[[Random[Integer, {1, Length[vars]}]]] =
> numbers[[Random[Integer, {1, Length[numbers]}]]]
> but it doesn't work.
> i hope you have an idea!
> thank you for answering!
> MFG Ben
===
Subject: Re: Assign a value to a variable
> Hi!
> I have a list of variables:
> vars = {a, b, c, d, e, f, g, h, i}
> and a list of numbers:
> numbers = Range[9]
> Now i'd like to choose randomly a variable and a number. and then i'd
> like to assign the number to the variable.
> I thought of somehting like that:
> vars[[Random[Integer, {1, Length[vars]}]]] =
> numbers[[Random[Integer, {1, Length[numbers]}]]]
> but it doesn't work.
> i hope you have an idea!
> thank you for answering!
> MFG Ben
Set[vars[[Random[Integer, {1, Length[vars]}]]],
numbers[[Random[Integer, {1, Length[numbers]}]]]];
vars
--> {a, b, c, d, e, f, 3, h, i}
Peter
===
Subject: Re: Assign a value to a variable
Hi Ben,
The operator Set has the Attribute HoldFirst. It therefore does not
evaluate the lefthand side of lhs = rhs.
You can force an evaluation by:
Evaluate[lhs]=rhs
Daniel
> Hi!
> I have a list of variables:
> vars = {a, b, c, d, e, f, g, h, i}
> and a list of numbers:
> numbers = Range[9]
> Now i'd like to choose randomly a variable and a number. and then i'd
> like to assign the number to the variable.
> I thought of somehting like that:
> vars[[Random[Integer, {1, Length[vars]}]]] =
> numbers[[Random[Integer, {1, Length[numbers]}]]]
> but it doesn't work.
> i hope you have an idea!
> thank you for answering!
> MFG Ben
===
Subject: Re: Assign a value to a variable
Clear[a,b,c,d,e,f,g,h,i];
vars={a,b,c,d,e,f,g,h,i};
numbers=Range[9];
Set has attribute HoldFirst so that the LHS is not evaluated
Attributes[Set]
{HoldFirst,Protected,SequenceHold}
Force evaluation of the LHS of Set
rn=
(Evaluate[vars[[
varsPos=
Random[Integer,{1,Length[vars]}]]]]=
numbers[[
Random[Integer,{1,Length[numbers]}]]]);
{rn, varsPos,vars}
{3,2,{a,3,c,d,e,f,g,h,i}}
rn==vars[[varsPos]]==
ToExpression[
ToCharacterCode[a]+varsPos-1]]
True
Bob Hanlon
===
> Subject: Assign a value to a variable
> Hi!
> I have a list of variables:
> vars = {a, b, c, d, e, f, g, h, i}
> and a list of numbers:
> numbers = Range[9]
> Now i'd like to choose randomly a variable and a number. and then i'd
> like to assign the number to the variable.
> I thought of somehting like that:
> vars[[Random[Integer, {1, Length[vars]}]]] =
> numbers[[Random[Integer, {1, Length[numbers]}]]]
> but it doesn't work.
> i hope you have an idea!
> thank you for answering!
> MFG Ben
===
Subject: Re: Re: Using The Random Function to predict Things
I have to make more corrections. Firstly, I meant, of course ,
until successive k heads come up. And secondly, one should start
nesting with a digital coin throw Random[Integer] rather than with
0, so the correct code is:
simulate[k_]:=NestWhileList[Random[Integer]&,Random[Integer],Plus[##]
=!=k&,k]
Andrzej
>> A simple way to simulate tossing a coin until three heads come up is:
>> simulate[k_]:=NestWhileList[Random[Integer]&,0,Plus[##]=!=k&,k]
> I meant until k heads come up.
> Andrzej Kozlowski
>> Hi there friends...
>> I used this line of code typicalList = Table[Random[Integer],
>> {50}] and got this result...
>> {1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1,
>> 0, 0, 0, 0,
>> 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0,
>> 1, 1, 0, 1}
>> By generating a few more of these random lists, I'm to predict a
>> similiar situation: such as how long i would expect to wait for
>> 3 heads in a coin tossing competition...
>> any suggestion or help as to what code i could use to do this?
> in this kind of sequence you've got 2^k possibilities of k
> subsequent
> numbers. One of them consits of k times the one. As 1 and 0 occur
> with
> the same probability, one would expect to wait on average 2(2^k-1)
> tosses of digits.
>> This answer is, of course, correct. In fact the answer to the
>> question how many flips of a coin are needed on the average to
>> get any specified pattern is well known and due to A. D. Solvev.
>> The whole problem is solved in detail in Knuth's book Concrete
>> Mathematics ( see particularly page 394 in the chapter Discrete
>> Probability).
>> A simple way to simulate tossing a coin until three heads come up is:
>> simulate[k_]:=NestWhileList[Random[Integer]&,0,Plus[##]=!=k&,k]
>> For example:
>> simulate[5]
>> {0,1,0,1,1,0,1,0,1,1,0,1,0,1,1,1,0,1,1,0,0,1,0,1,0,0,1,0,0,1,0,1,0,0,
>> 0,0,0,1,
>> 1,1,0,0,0,1,1,1,0,1,1,0,0,0,0,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,0,0,1,0,1
>> ,0,0,1,0,
>> 0,1,1,0,1,1,0,0,1,0,0,0,0,1,1,0,1,0,1,0,1,0,1,1,1,0,0,1,1,1,1,0,0,1,0
>> ,1,0,0,0,
>> 1,1,1,0,0,1,1,1,1,0,1,1,0,0,1,1,1,1,1}
>> Andrzej Kozlowski
===
Subject: Re: Re: Using The Random Function to predict Things
> A simple way to simulate tossing a coin until three heads come up is:
> simulate[k_]:=NestWhileList[Random[Integer]&,0,Plus[##]=!=k&,k]
I meant until k heads come up.
Andrzej Kozlowski
> Hi there friends...
> I used this line of code typicalList = Table[Random[Integer],
> {50}] and got this result...
> {1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1,
> 0, 0, 0, 0,
> 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1,
> 1, 0, 1}
> By generating a few more of these random lists, I'm to predict a
> similiar situation: such as how long i would expect to wait for 3
> heads in a coin tossing competition...
> any suggestion or help as to what code i could use to do this?
>> in this kind of sequence you've got 2^k possibilities of k subsequent
>> numbers. One of them consits of k times the one. As 1 and 0 occur
>> with
>> the same probability, one would expect to wait on average 2(2^k-1)
>> tosses of digits.
> This answer is, of course, correct. In fact the answer to the
> question how many flips of a coin are needed on the average to
> get any specified pattern is well known and due to A. D. Solvev.
> The whole problem is solved in detail in Knuth's book Concrete
> Mathematics ( see particularly page 394 in the chapter Discrete
> Probability).
> A simple way to simulate tossing a coin until three heads come up is:
> simulate[k_]:=NestWhileList[Random[Integer]&,0,Plus[##]=!=k&,k]
> For example:
> simulate[5]
> {0,1,0,1,1,0,1,0,1,1,0,1,0,1,1,1,0,1,1,0,0,1,0,1,0,0,1,0,0,1,0,1,0,0,0
> ,0,0,1,
> 1,1,0,0,0,1,1,1,0,1,1,0,0,0,0,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,0,0,1,0,1,
> 0,0,1,0,
> 0,1,1,0,1,1,0,0,1,0,0,0,0,1,1,0,1,0,1,0,1,0,1,1,1,0,0,1,1,1,1,0,0,1,0,
> 1,0,0,0,
> 1,1,1,0,0,1,1,1,1,0,1,1,0,0,1,1,1,1,1}
> Andrzej Kozlowski
===
Subject: Re: Re: Using The Random Function to predict Things
>> Hi there friends...
>> I used this line of code typicalList = Table[Random[Integer],
>> {50}] and got this result...
>> {1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0,
>> 0, 0, 0,
>> 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1,
>> 1, 0, 1}
>> By generating a few more of these random lists, I'm to predict a
>> similiar situation: such as how long i would expect to wait for 3
>> heads in a coin tossing competition...
>> any suggestion or help as to what code i could use to do this?
> in this kind of sequence you've got 2^k possibilities of k subsequent
> numbers. One of them consits of k times the one. As 1 and 0 occur with
> the same probability, one would expect to wait on average 2(2^k-1)
> tosses of digits.
This answer is, of course, correct. In fact the answer to the
question how many flips of a coin are needed on the average to get
any specified pattern is well known and due to A. D. Solvev. The
whole problem is solved in detail in Knuth's book Concrete
Mathematics ( see particularly page 394 in the chapter Discrete
Probability).
A simple way to simulate tossing a coin until three heads come up is:
simulate[k_]:=NestWhileList[Random[Integer]&,0,Plus[##]=!=k&,k]
For example:
simulate[5]
{0,1,0,1,1,0,1,0,1,1,0,1,0,1,1,1,0,1,1,0,0,1,0,1,0,0,1,0,0,1,0,1,0,0,0,0
,0,1,
1,1,0,0,0,1,1,1,0,1,1,0,0,0,0,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,0,0,1,0,1,0,
0,1,0,
0,1,1,0,1,1,0,0,1,0,0,0,0,1,1,0,1,0,1,0,1,0,1,1,1,0,0,1,1,1,1,0,0,1,0,1,
0,0,0,
1,1,1,0,0,1,1,1,1,0,1,1,0,0,1,1,1,1,1}
Andrzej Kozlowski
===
Subject: Re: A list of numbers without 73
Did you try the built in function DeleteCases?
--
http://chrischiasson.com/contact/chris_chiasson
===
Subject: Re: A list of numbers without 73
> Experts,
> S(i) should be a list of positive integers without the substring 73.
> My code seems to be very inefficient. I multiply all numbers with 0 if
they
> contain 73, sort the new list, make the union (remove double elements)
and
> delete the first element. Therefore it is necessary to use an if statement
> in order not to delete the first element of the numbers 1-9. Any more
> sophisticated ideas?
> L := Table[k, {k, 1, 9}]
> S[i_] := S[i] =
> If[i == 1, L,
> Delete[Union[
> Table[k*(1 - Boole[StringMatchQ[ToString[k], *73*]]), {k,
> 10^(i - 1), 10^i - 1}]], 1]]
> Best
> Thomas
Hi Thomas,
it seems to be more efficient to construct only numbers without the
sequence 73 instead of testing already existing ones:
s2[1] = Range[1, 9];
s2[i_] := Flatten[(10*#1 + If[Mod[#1, 10] == 7,
{0, 1, 2, 4, 5, 6, 7, 8, 9},
Range[0, 9]] & ) /@ s2[i - 1]]
Timing[Length[#1[6]]]& /@ {S, s2}
--> {{7.765*Second, 854569}, {0.953*Second, 854569}}
Ceers,
Peter
===
Subject: Re: A list of numbers without 73
>>Experts,
>>S(i) should be a list of positive integers without the substring 73.
>>My code seems to be very inefficient. I multiply all numbers with 0 if
they
>>contain 73, sort the new list, make the union (remove double elements)
and
>>delete the first element. Therefore it is necessary to use an if statement
>>in order not to delete the first element of the numbers 1-9. Any more
>>sophisticated ideas?
>>L := Table[k, {k, 1, 9}]
>>S[i_] := S[i] =
>>If[i == 1, L,
>>Delete[Union[
>>Table[k*(1 - Boole[StringMatchQ[ToString[k], *73*]]), {k,
>>10^(i - 1), 10^i - 1}]], 1]]
>>Best
>>Thomas
>>
> I don't know about the timing issues inherent with using Select
>(does it check _each_ member of its enclosed list; might, perhaps, an if
>statement for the power i used in Range be more efficient? I'm too
>lazy to do rigorous checks), but this should work for you:
>S[i_] := Select[Range[10^(i - 1), 10^i - 1], #1 != 73 & ]
>In[32]:=
>$Version
>Out[32]=
>5.2 for Linux (June 20, 2005)
> Best of luck,
> C.O.
OK, so I'm not quite that lazy. A quick logarithmic regression on
fresh kernels gives the evaluation time for your function of roughly
time(i) ~ Exp[-11.54+2.273 *i], in seconds.
My (unrefined) function seems to be roughly 10x as fast (for
exponents -- or values of i -- around 2 to 5), to around 7.5x as fast
(for values of i around 8) , probably due to not having to do
string-matching stuff.
C.O.
===
Subject: Re: A list of numbers without 73
>Experts,
>S(i) should be a list of positive integers without the substring 73.
>My code seems to be very inefficient. I multiply all numbers with 0 if they
>contain 73, sort the new list, make the union (remove double elements)
and
>delete the first element. Therefore it is necessary to use an if statement
>in order not to delete the first element of the numbers 1-9. Any more
>sophisticated ideas?
>L := Table[k, {k, 1, 9}]
>S[i_] := S[i] =
>If[i == 1, L,
>Delete[Union[
>Table[k*(1 - Boole[StringMatchQ[ToString[k], *73*]]), {k,
>10^(i - 1), 10^i - 1}]], 1]]
>Best
>Thomas
I don't know about the timing issues inherent with using Select
(does it check _each_ member of its enclosed list; might, perhaps, an if
statement for the power i used in Range be more efficient? I'm too
lazy to do rigorous checks), but this should work for you:
S[i_] := Select[Range[10^(i - 1), 10^i - 1], #1 != 73 & ]
In[32]:=
$Version
Out[32]=
5.2 for Linux (June 20, 2005)
Best of luck,
C.O.
===
Subject: Re: A list of numbers without 73
> Experts,
> S(i) should be a list of positive integers without the substring 73.
> My code seems to be very inefficient. I multiply all numbers with 0 if
they
> contain 73, sort the new list, make the union (remove double elements)
and
> delete the first element. Therefore it is necessary to use an if statement
> in order not to delete the first element of the numbers 1-9. Any more
> sophisticated ideas?
> L := Table[k, {k, 1, 9}]
> S[i_] := S[i] =
> If[i == 1, L,
> Delete[Union[
> Table[k*(1 - Boole[StringMatchQ[ToString[k], *73*]]), {k,
> 10^(i - 1), 10^i - 1}]], 1]]
A simpler approach is to use Pick:
S[i_] := Module[{rng = Range[10^(i - 1), 10^i - 1]},
Pick[rng, StringMatchQ[ToString /@ rng, *73*], False]]
This version seems to be about 2.5 times faster. Most of the time is
taken up in using ToString to convert the numbers into strings.
Carl Woll
Wolfram Research
> Best
> Thomas
===
Subject: Re: A list of numbers without 73
I forgot to say that S(i) is a list of positive integers with i digits.
Best,
Thomas
Thomas Schmelzer schrieb im Newsbeitrag
> Experts,
> S(i) should be a list of positive integers without the substring 73.
> My code seems to be very inefficient. I multiply all numbers with 0 if
> they
> contain 73, sort the new list, make the union (remove double elements)
> and
> delete the first element. Therefore it is necessary to use an if
statement
> in order not to delete the first element of the numbers 1-9. Any more
> sophisticated ideas?
> L := Table[k, {k, 1, 9}]
> S[i_] := S[i] =
> If[i == 1, L,
> Delete[Union[
> Table[k*(1 - Boole[StringMatchQ[ToString[k], *73*]]), {k,
> 10^(i - 1), 10^i - 1}]], 1]]
> Best
> Thomas
===
Subject: Re: Re: How to hide a cell?
> If you make a cell hidden that way (including making it non-selectable),
> then am I correct that the only way to recover it is to edit the
> notebook as an expression and manually change that cell's options?
We can set different cell properties for different envorinment, such
as only hide these cell in Printing env.
--
Li Zhengji
-------------------------------------------------------------
If all you have is a hammer, everything is a nail.
-------------------------------------------------------------
===
Subject: Re: How to hide a cell?
Zhengji,
In order to make a cell totally invisible, we must use three options. Here
goes an example:
invisibleCell = Cell[Invisible, Input,
CellElementSpacings -> {ClosedCellHeight -> 0.`},
CellOpen -> False,
ShowCellBracket -> False];
NotebookWrite[InputNotebook[], invisibleCell]
Carlos C.8esar de Ara.9cjo
Gregos & Troianos Educacional
www.gregosetroianos.mat.br
Belo Horizonte, MG, Brasil
(31) 3283-1122
===
Subject: Re: UML-like diagrams for Mathematica
One problem with trying to find structure in symbols is that rules
which can only work with a certain symbol may not even be associated
with that symbol.
cf: y[x]=expr ... the rule is only associated with y, but will only
work if both y and x are present.
It seems like symbols don't have any structure besides context (the
stuff in front of `) (I would guess symbols themselves are Atomic).
My guess is that a symbol is really defined by what rules are
eventually applied to it.
P.S. Calling SetOptions seems much like calling Set. Neither of these
commands actually prevents you from doing what you want with a
particular symbol. They just operate on lists of rules that are used in
default transformations. One may override SetOptions with an option
in the function call. One may override Set with Block.
--
http://chrischiasson.com/contact/chris_chiasson
===
Subject: Re: Types in Mathematica
> I meant message in the sense of Message[Arguments], from now on when
> talking about Mathematica expressions I'll capitalize the first letter
> and use lower case when some other meaning is intended. Rereading
> these messages though, I notice that I'm a poor editor of emails so
> there will be mistakes.
Oh, now I see what you meant. Part[Complex, 1]. To be pedantic, it doesn't
actually return the Message. It displays it, and returns
Complex[LeftDoubleBracket]1[RightDoubleBracket].
> I'm talking about
> creating things as composite expressions and treating them _as if_ they
> were atomic.
> Perhaps doing this in pursuit of a specific application would be
> helpful. I've notices the occasional relativist (the physics type not
> the philosophic type) who is looking to use Mathematica to represent
> tensors and the other structures they work with symbolically. I
> believe there is a package that does so already, but if there is a
> candidate for something that should act like and Atom but is not one I
> suppose that's it. I know far too little of what researchers in
> relativity do to be able to pursue that idea further.
That is actually one of my primary interests. Interestingly, I have
discovered the formulae describing hurricanes look a whole lot like those in
general relativity. That was the impetus for my returning to Mathematica.
WRT tensor packages, the moderator of this newsgroup created a commercial
package called MathTensor. It's pricy. At least for my current situation.
I'm not sure why the Unix version (I assume that includes Linux) costs $230
more than the $570 price for the Mac and Windows versions. When I feel
confident with my Mathematica skills, I may consider buying it.
I don't expect it to be OOP based.
> Many modern languages have rejected the type system used by the various
> C and Algol derived languages: Haskell, ML, Mercury, Prolog, Dylan,
> Common Lisp... I know a lot of people are happy with C++ but as far as
> I can tell from programming language designers it is not really an
> interesting path to follow.
It's also a hard act to follow. D is the closest thing I know of to a
serious
effort to improve on the C++ principles. You can find more here:
http://digitalmars.com/d/comparison.html
C++ has some problems. Many of them have to do with the fact that it
strives
to be C compatable. One thing I've learned from studying C++ is that the
builtin features of other languages can usually be implemented or emulated
in
C++. In many respects, it's good that C++ hasn't incorporated every
newfangled idea that's come along. It really is intended to be a basis for
building complete development systems, and not such a system. IMO, one of
the biggest problems of C++ is the fact that too many people treat it like
C.
> Was it just a question of who could yell loudest or pout longest, or
> were there solid reasons for not taking that approach?
> I suggest reading up on Hindley-Milner type systems, they seem to be
> the rage in statically typed functional languages.
It would seem a likely candidate for Mathematica. I don't know if you were
suggesting a reason why there isn't a comprehensive type system in
Mathematica.
> What would happen if users could define Atom-like types? That is,
> types
> that only expose their content through an interface. Whoops! That's
> OOP!
> There's nothing inherently wrong with OOP but it achieved prominence on
> the back of C and Algol like languages and those implementations of OOP
> systems create issues that other languages simply don't have. For
> example at least one person
> (http://norvig.com/design-patterns/ppframe.htm) thinks the Gang of Four
> would not be earning a dime of royalty or consulting fees if they used
> a dynamically typed language because most of their patters are trivial
> in other idioms.
The web browser I used to access his powerpoint slides was written in C++.
PowerPoint (AFAIK) was written in C++. The mail client I am using was
written in C++. The window manager I'm using was written in C++. His
critique was written a decade ago.
> What determines the value is the evaluator, what holds it is some
> expression, in this case one with the head Real.
does the evaluator obtain the precursor values for calculating the returned
value of Arg[s]?
> They _are_ the rewrite rules. At least from the user's perspective.
> Do you
> not agree? See 2.6.2 in the Help Browser.
> I agree that that is what they are, and their existence makes it
> possible to write self modifying code pretty easily should the need
> arise. But they are not necessary.
What are the alternatives without significantly modifying the language?
> I am willing to bet they are simply bits in a single int variable in
> the
> Symbol struct. What the consequences of their being there are, is a
> different story. They may well be arguments to a switch statement.
> Again we're thinking on different levels, I'm not really interested in
> what the implementation of Mathematica looks like (I understand it's a
> blend of C and Mathematica itself). I'm interested in how one would
> model the evaluator in the abstract.
I don't believe you will in general find easy ways using Rule[] to produce
behavior equivalent to that which results from Attributes being set. It way
I view the evaluation is that the evaluator encounters the symbol and
queries
it for certain kinds of data relevant to the current state of the evaluator.
There is a set of rules to determine the next state depending on the values
obtained from querying the symbol and the current state of the evaluator.
After obtaining all the values, the rules are applied, and the evaluator
transitions to the next state.
> In my job our product is a blend of an interpreted language and an
> internal implementation in another language and I can honestly say that
> there would be no enlightenment for someone seeking to understand the
> interpreted language to look at its implementation.
I really don't care so much whether I know how Mathematica is implemented in
the Kernel. I am, however, interested in an abstract model of that
implementation. The fact of the matter is, I would probably not like the
way
Mathematica is written. At least if the MathLink code is any indicator.
> I don't agree. The items I listed are the abstract data members of the
> type Symbol. The evaluator doesn't have rewrite rules any more
than
> a
> compiler has function definitions. The rules exist in the expression
> structure passed to the evaluator.
> Regardless of how the evaluator actually works, the question is how you
> can reason about it accurately with respect to generating the correct
> output for a given input (even if you get the algorithmic complexity
> wrong). I submit that the correct way to think about it is as a
> rewrite system, as others have said.
That is exactly what a CPU is. But viewing it as simply a rewrite system
does
not help much when the necessary knowledge determing how it works is
scattred
throughout a 1500 page book.
It would be nice if that level of functionality were explained in a more
coherent and structured way. I have books on every language from Old Norse
to the bash shell. They almost all have some kind of tree diagram
representing grammatical structures. Those tools are completely applicable
to Mathematica, but I have _never_ seen them used.
Steven
===
Subject: Re: Types in Mathematica
> Atoms really do seem to satisfy the definition of abstract data type.
> One
> might even say they satisfy the definition of object in the OOP sense.
Sort of, they are a decent candidate.
>> Part cannot take anything beyond the 0th
>> (head) element of atoms, Part returns a Message
> I'm not sure I would use the term Message here. But then, message
> is
> something I consider specific to SmallTalk, and not generally useful
> when
> discussing OOP.
I meant message in the sense of Message[Arguments], from now on when
talking about Mathematica expressions I'll capitalize the first letter
and use lower case when some other meaning is intended. Rereading
these messages though, I notice that I'm a poor editor of emails so
there will be mistakes.
> I'm talking about
> creating things as composite expressions and treating them _as if_ they
> were atomic.
Perhaps doing this in pursuit of a specific application would be
helpful. I've notices the occasional relativist (the physics type not
the philosophic type) who is looking to use Mathematica to represent
tensors and the other structures they work with symbolically. I
believe there is a package that does so already, but if there is a
candidate for something that should act like and Atom but is not one I
suppose that's it. I know far too little of what researchers in
relativity do to be able to pursue that idea further.
> It appears Maeder would have had a stronger type
> system in Mathematica. [deleted] I suspect there were
> discussion about this sort of thing in the early days, and the notion
> of a
> type system along the lines of C++ was rejected.
Many modern languages have rejected the type system used by the various
C and Algol derived languages: Haskell, ML, Mercury, Prolog, Dylan,
Common Lisp... I know a lot of people are happy with C++ but as far as
I can tell from programming language designers it is not really an
interesting path to follow.
> Was it just a question of who could yell loudest or pout longest, or
> were there solid reasons for not taking that approach?
I suggest reading up on Hindley-Milner type systems, they seem to be
the rage in statically typed functional languages.
> What would happen if users could define Atom-like types? That is,
> types
> that only expose their content through an interface. Whoops! That's
> OOP!
There's nothing inherently wrong with OOP but it achieved prominence on
the back of C and Algol like languages and those implementations of OOP
systems create issues that other languages simply don't have. For
example at least one person
(http://norvig.com/design-patterns/ppframe.htm) thinks the Gang of Four
would not be earning a dime of royalty or consulting fees if they used
a dynamically typed language because most of their patters are trivial
in other idioms.
>> The value of Arg[1+I] is Arg[1+I]
> Are you talking about the pattern, or the result given by evaluating
> the
> expression?
Both since an equation is a tautology. A better way at looking at
values in a rewrite system though is a value is what happens when there
are no more rewrite rules left after applying them to the initial
expression in which case the value in Mathematica is
Times[Rational[1,4],Pi].
Similarly the value of Unevaluated[Arg[1+I]] is Arg[1+I] since you have
to submit the argument of Unevaluated to the evaluator again.
> Indeed, another pit of vipers I seem to have stepped into. What I
> should
> have written was What determines (holds) the value of Arg[1+I]//N?
What determines the value is the evaluator, what holds it is some
expression, in this case one with the head Real.
>> Since there are rewrite
>> rules associated with Arg one has to be careful about when they look
>> at an Arg expression depending on what they want to do with it.
> This is certainly a point of frustration I have with Mathematica. If
> I look
> at the documentation for Arg[], I am not given a comprehensive
> description
> of its behavior.
A large part of the Mathematica Book is dedicated to describing the
evaluator, it would be redundant to repeat it every time.
>> While we continue on our tour of programming languages the
>> logic languages
> ?? What do you mean here? Can you provide an example? I have been
> glancing at SML over the past few days. I also compiled and crashed
> Axiom.
I was thinking of languages similar to Prolog, but this is aside from
the topic of this thread.
> UpValues
> DownValues
> OwnValues
> SubValues
>> These things are side effects of the evaluator, like In and Out, not
>> fundamental. They are very useful though because they describe
>> expressions we can use to interact with the rewrite rules used by the
>> evaluator itself.
> They _are_ the rewrite rules. At least from the user's perspective.
> Do you
> not agree? See 2.6.2 in the Help Browser.
I agree that that is what they are, and their existence makes it
possible to write self modifying code pretty easily should the need
arise. But they are not necessary.
>> Attributes are definitely an interesting beast, I'm not sure but I
>> suspect they can be considered a special set of rules used by the
>> evaluator.
> I am willing to bet they are simply bits in a single int variable in
> the
> Symbol struct. What the consequences of their being there are, is a
> different story. They may well be arguments to a switch statement.
Again we're thinking on different levels, I'm not really interested in
what the implementation of Mathematica looks like (I understand it's a
blend of C and Mathematica itself). I'm interested in how one would
model the evaluator in the abstract.
In my job our product is a blend of an interpreted language and an
internal implementation in another language and I can honestly say that
there would be no enlightenment for someone seeking to understand the
interpreted language to look at its implementation.
> I don't agree. The items I listed are the abstract data members of the
> type Symbol. The evaluator doesn't have rewrite rules any more than
> compiler has function definitions. The rules exist in the expression
> structure passed to the evaluator.
Regardless of how the evaluator actually works, the question is how you
can reason about it accurately with respect to generating the correct
output for a given input (even if you get the algorithmic complexity
wrong). I submit that the correct way to think about it is as a
rewrite system, as others have said.
Ssezi
===
Subject: Re: Types in Mathematica
>> Let's assume Vector3d has a conversion operator that returns a string
>> representation. I could simply pass it to an output stream that takes a
>> string type, and the Vector3d would be converted to a string and put
onto
>> the stream. No need to explicitly call a conversion function
explicitly.
> Overloading is simply done in a different way in Mathematica, e.g. using
> inexhaustive pattern matches. This does not make Mathematica less
powerful
> than C++.
In C++ overloading is based on types. There are different parts of the
language that participate in this behavior. For the most part, it depends
on function call signature matching. The advantage is that I don't need to
know the details of everything that can convert to a whatsit when I write a
function that takes a whatsit as an argument. Similarly, I don't need to
know about every function that takes a whatsit when I create a type
conversion operator for a whosit class. Since Mathematica has no concept
of function call signature, that cannot be done. I have to do something
that explicitly makes the connection between the function and the argument.
Either I place the argument pattern in the downvalues of the function, or I
put it in the up-values of the argument. In the former, the function
specifically references the argument type. In the latter, the argument
type specifically references the function.
There is a slight advantage that Mathematica up-values have over C++
operator overloading. In C++ there is no associated access specifier
(something I have suggested to the C++ design committee) that would simply
attach a particular operator to the class upon which it operates without
granting it any privileged access. Unfortunately, trying to explain what I
mean by this, and also how it would be beneficial proved inhibitive. The
few people who did understand the idea (including Stroustrup) didn't find
it worth retrofitting to the existing language.
In the case of up-values, they are attached to the Symbol to which they
apply. But in general, overloading in Mathematica is not as flexible as it
is in C++.
--
The Mathematica Wiki: http://www.mathematica-users.org/
Math for Comp Sci http://www.ifi.unizh.ch/math/bmwcs/master.html
Math for the WWW: http://www.w3.org/Math/
===
Subject: Re: Types in Mathematica
>> But a Complex will always have a real and an imaginary component.
>> A Rational
>> will always have a numerator and a denominator.
> And a List always contains zero or more expressions.
>> I don't see that to be containment in the same sense as a Complex
>> contains
>> its values.
> You're right it's not exactly the same because Complex is an atom and
> a list is not.
Atoms really do seem to satisfy the definition of abstract data type. One
might even say they satisfy the definition of object in the OOP sense.
>> expression/symbol with the head Complex.
> Only an expression can have the head Complex, a symbol has the head
> Symbol and to prevent confusion I think we should agree to keep that
> straight.
attempt a UML class diagram of Mathematica data types. It will indeed be
rooted with expression. The problem with that model is that we never have
a thing we refer to as Expression within the language.
>> Even if we do view an
>> expression with the head List as a data structure containing elements
>> (often called arguments), we are still able to describe it in terms
>> of the
>> functions which can operate on it, as well as the data abstraction it
>> represents.
> Of which the abstraction is what?
Well, do you _need_ to know the details of how it is implemented in order
to
use it? List is kind of like a brown paper wrapper expression. Its
head
is a Symbol named List which has certain attributes. Beyond that,
whatever else it does seems to be determined by what it inherits from
Expression. I'll have more to say on this soon...I hope.
>> When using expressions to represent ADTs we usually don't want to
>> simply
>> extract values using Part. We want to treat it as if we do not
>> know the
>> internal structure. We only know the interface.
> I don't think I agree with this, we know several things about Part
> with respect to expressions:
That's not the point. I'm talking about a protocol based on mutual
agreement, not a law enforced by software (though there are ways of hiding
internal structure of an ADT - or so Maeder claims.) This is similar to
the way SIMULA originally handled data abstraction. It's as if we
implemented our C++ classes as struct with all public data members, and
simply agreed to only use the member functions to access the internal
structure. In principle you /could/ access the member data, but that would
violate the design principle.
> Part cannot take anything beyond the 0th
> (head) element of atoms, Part returns a Message
I'm not sure I would use the term Message here. But then, message
is
something I consider specific to SmallTalk, and not generally useful when
discussing OOP.
> when the index is
> greater than the depth of the expression being taken apart, Part
> returns the expression corresponding to an argument of the larger
> expression when 0 < the second argument to part <= Depth[first
> argument]. If you consider the arguments to be internal structure
> then the behavior of Part makes it necessary that you treat
> expressions as if you know something about their internals. It also
> makes clear what an atom is: a Depth 0 object.
What you are describing is the interface to Expression. I'm talking about
creating things as composite expressions and treating them _as if_ they
were atomic.
>> You cannot extract the components of an expression with head
>> Complex using
>> Part[].
> Because by definition you cannot do that to any atom and an
> expression of the form Complex[_,_] is an atom.
>> You have to ask nicely. For Lists, I believe that Part[] /is/
>> asking nicely.
> I don't consider this exceptional, all you have to do is distinguish
> the behavior of Part between things that are atoms and things that
> are not, all the rest follows logically.
The point I'm trying to make is that Atoms are staticly defined data types.
In reality, they are considered constants. You cannot change the value
of/in an Atom. You can only replace the whole Atom. With an Expression,
you /can/ change its contents.
>> I just found this and have only glanced at it, but I believe it
>> will give a
>> good context for further discussion on this matter:
>> http://library.wolfram.com/infocenter/Conferences/4680/
I did read it quickley. It appears Maeder would have had a stronger type
system in Mathematica. This is what I was really trying to get at when I
started this thread ... back in the Early Modern Era. I suspect there were
discussion about this sort of thing in the early days, and the notion of a
type system along the lines of C++ was rejected. Was it just a question of
who could yell loudest or pout longest, or were there solid reasons for not
taking that approach?
What would happen if users could define Atom-like types? That is, types
that only expose their content through an interface. Whoops! That's OOP!
One of the bigest problems, as I see it, is the lack of automatic type
conversion.
>> What determines (holds) the value of Arg[1+I]?
> The value of Arg[1+I] is Arg[1+I]
Are you talking about the pattern, or the result given by evaluating the
expression?
> there are also rewrite rules for
> equivalent expressions where equivalence is defined by expr1==expr2
> being congruent to True eg:
> In[54]:=
> FullForm[Arg[1 + I]]
> Arg[1 + I] == Times[1/4, Pi]
> Out[54]//FullForm=
> Times[Rational[1,4],Pi]
> Out[55]=
> True.
Indeed, another pit of vipers I seem to have stepped into. What I should
have written was What determines (holds) the value of Arg[1+I]//N?
>> Where does it live?
> In the space of arguments to the evaluator. Since there are rewrite
> rules associated with Arg one has to be careful about when they look
> at an Arg expression depending on what they want to do with it.
This is certainly a point of frustration I have with Mathematica. If I
look
at the documentation for Arg[], I am not given a comprehensive description
of its behavior.
> There are actually some very interesting theoretical on relations and
> how then end up being implemented in RDBMSes (short version,
> poorly).
Not sure exactly what you mean here. If you are talking about RDBMS vs.
say
ODBMS, I haven't seen much come out of ODBMS which I found useful. The
idea is attractive, but the reality becomes quite a mess. I really wasn't
suggesting we could, or should, try to describe Mathematica things as
entries in an RDBMS, I would merely using the example in order to motivate
the analogy of an axis.
> While we continue on our tour of programming languages the
> logic languages
?? What do you mean here? Can you provide an example? I have been
glancing at SML over the past few days. I also compiled and crashed Axiom.
> would be appropriate examples for reasoning about
> relations. And they too bear a strong relationship (pun not
> intended) to LISP like languages.
I suspect most languages that strive to capture the essence of mathematical
reasoning will be expressed in a prefix functional form similar to
Mathematica's internal form. It may not be easy to read or write
expressions in that form, but it makes writting the evaluator much simpler.
Since Lisp already works this way (moving the head inside the '(',')'), the
will have similarities to Lisp.
>> UpValues
>> DownValues
>> OwnValues
>> SubValues
> These things are side effects of the evaluator, like In and Out, not
> fundamental. They are very useful though because they describe
> expressions we can use to interact with the rewrite rules used by the
> evaluator itself.
They _are_ the rewrite rules. At least from the user's perspective. Do
you
not agree? See 2.6.2 in the Help Browser.
> Attributes are definitely an interesting beast, I'm not sure but I
> suspect they can be considered a special set of rules used by the
> evaluator.
I am willing to bet they are simply bits in a single int variable in the
Symbol struct. What the consequences of their being there are, is a
different story. They may well be arguments to a switch statement.
>> internal data
> I'm not sure internal data is any more worth worrying about to a
> Mathematica programmer as a naked quark is to a nuclear engineer.
You may not be interested in the details, but knowing that there is data in
there, and what kind of data abstraction it represents, is certainly
useful.
>> messages
> Message are just expressions, which is why you can trace on Message
> [___] to stop evaluation at a point a message is issued. Messages
> are not atomic.
It appears there is actually an Expression Messages associated with a
Symbol that has messages, so it might be meaningful to view them as member
data of the Symbol. The are certainly not accessed using a part
specification.
>> Context (symbol is probably best understood as a fully qualified
>> name.)
> Likewise a Context is just another expression whose value is a side
> effect of the evaluator (in particular of Get[] and Needs[]
> expressions).
Contexts are Strings Head[Context[Arg]]. That's why they need to be quoted
when given as arguments to expressions.
> To reason about these things is to reason about the
> rewrite rules of the evaluator, but is aside from the idea of types.
I don't agree. The items I listed are the abstract data members of the
type Symbol. The evaluator doesn't have rewrite rules any more than a
compiler has function definitions. The rules exist in the expression
structure passed to the evaluator.
--
The Mathematica Wiki: http://www.mathematica-users.org/
Math for Comp Sci http://www.ifi.unizh.ch/math/bmwcs/master.html
Math for the WWW: http://www.w3.org/Math/
===
Subject: Re: Assign a value to a variable
Ben,
The quick answer is to Evaluate the lhs of the Set statement.
vars = {a, b, c, d, e, f, g, h, i};
numbers = Range[9];
Evaluate[vars[[Random[Integer,
{1, Length[vars]}]]]] =
numbers[[Random[Integer, {1, Length[numbers]}]]]
vars
But are you going to evaluate it repeatedly? Then you will get an error if
a
variable is already assigned. You could do something like this...
randomAssign[vars_?VectorQ, nums_?VectorQ] :=
Module[
{ranvar = Random[Integer, {1, Length[vars]}],
rannum = Random[Integer, {1, Length[vars]}],
x, y},
y = Part[vars, ranvar];
x = Part[nums, rannum];
]
randomAssign[vars, numbers]
vars
Or maybe you want to Drop the entries from each of the lists with each
assignment. You could then use the following...
ClearAll[randomAssign];
Attributes[randomAssign] = {HoldAll};
randomAssign[vars_?VectorQ, nums_?VectorQ] :=
Module[
{ranvar, rannum, x, y},
If[Length[vars] == 0 [And] Length[nums] == 0, Return[]];
ranvar = Random[Integer, {1, Length[vars]}];
rannum = Random[Integer, {1, Length[vars]}];
y = Part[vars, ranvar];
x = Part[nums, rannum];
Evaluate[y] = x;
vars = Drop[vars, {ranvar}];
nums = Drop[nums, {rannum}];
]
Clear[a, b, c, d, e, f, g, h, i]
vars = {a, b, c, d, e, f, g, h, i};
numbers = Range[9];
randomAssign[vars, numbers]
{a, b, c, d, e, f, g, h, i}
vars
The last statements can be evaluated repeatedly until all variables are
assigned.
David Park
djmp@earthlink.net
http://home.earthlink.net/~djmp/
Hi!
I have a list of variables:
vars = {a, b, c, d, e, f, g, h, i}
and a list of numbers:
numbers = Range[9]
Now i'd like to choose randomly a variable and a number. and then i'd
like to assign the number to the variable.
I thought of somehting like that:
vars[[Random[Integer, {1, Length[vars]}]]] =
numbers[[Random[Integer, {1, Length[numbers]}]]]
but it doesn't work.
i hope you have an idea!
thank you for answering!
MFG Ben
===
Subject: Simple task with Mathematica
I am currently learning how to use Mathematica at college with a view to
purchasing the program.
Having spent a week studying the documentation / Book etc of version 4 I
am struggling to do even the most simple tasks. I have looked at other
Math programs, but I am really impressed with the numerical precision &
scope of Mathematica. Below is an example of a really easy task which I
can do in a second in Excel, however I am stuck with Mathematica 4.
your attention.
Question:-
Given the two columns below; I am trying to take the first column (A)
created as a list & iterate it by starting with 5 to finish with the
second column using the function A(1) = (6.5*2)-5 (starting value from
the second column) = 8.
Then, for A(2) = (9.5*2)-8 =11 etc, etc giving the second column.
Function A(x) = x*2.
(A)rea
5
6.5
8
9.5
11
8.5
6
4.5
3
9
15
12.5
10
12.5
15
17.5
20
18.5
17
11
5
I have tried indexing with x = Range[11] & using A[[x]]-B[[x-1]]. Sorry,
but I am really confused.
Lea Rebanks.