tomleslie

13876 Reputation

20 Badges

15 years, 175 days

MaplePrimes Activity


These are replies submitted by tomleslie

map( op[1,1],[solve({.3707752782+.1499320455*sin(theta[4](t))+.1117559025*cos(theta[4](t))=0.5,
                                  theta[4](t)>=0,
                                  theta[4](t)<=Pi
                               },
                                theta[4](t)
                             )
                    ]
      )[];

Whilst producing a solution for Project Euler Problem 55 (aka Lychrel numbers) a while ago,I came to the conclusion that

revNum:= proc( x::integer )
                local p:=length(x),
                        y:=x;
                return add( irem(y, 10, 'y')*10^(p-k),
                                  k = 1..p                        
                                );
                end proc;

reverses integers faster than

f1:=x->parse(StringTools:-Reverse(convert(x,string)));

for any integer length up to ~20 digits

OP - please don't use anything from my previoous post: you are going to get incorrect answers - my bad

Following is addressed to Joe Riel: trying to work out why I am wrong.

I am deeply puzzled.

I eventually "stripped down" the problem to the following code sequence

  restart:
  C := Iterator:-Combination(20,10):
  B:= seq([c, convert(c,set)], c in C)[]:
  B[1..3][];

Now to my simple understanding, each entry in the list B[1..3][], ought to be [c as an Array, c as a set]. So same entries, just different "containers". However this is not the case. The entries in 'c' have changed,

So my fundamental question is - how can [c, convert(c,set)] produce different entries.

I mean if I write

a:=Array([1,2,3]);
[a, convert(a,set)];

I get two different containers, but the entries are the same. so in the assignment of 'B' above, ie

B:= seq([c, convert(c,set)], c in C)[]:

when exactly did the entries in 'c' change?????

My intention was to illustrate that the two parts of the OP's requirement which slowed things down, were

  1. The requirement that the output be a list of sets, rather than a list of Arrays
  2. The requirement that the combinations be chosen from 1..20, rather than 0..19. In other words, how much does the '+1' cost

Summary of timings for my machine

  1. Returning a list of Arrays on the combinations of integers 0..19 takes 0.4 secs
  2. Returning a list of sets on the combinations of integers 0..19, takes 0.9 secs
  3. Returning a list of sets on the combinations of integers 1..20 takes 1.4secs

Code snips for these three options below

# Option 1
#
# My original suggestion, returns a list (which was a matter
# of convenience only) of 1-D arrays
#
# 1. produces combinations of 0..19
# 2. takes about 0.4 secs
#
  restart;
  t1:=time():
  C := Iterator:-Combination(20,10):
  B:= [seq(c, c in C)]:
  t2:=time()-t1;
  whattype(B);
  numelems(B);
  B[1];
  whattype(B[1]);

# Option 2
#
# Modifying the above to produce a list of sets
#
# 1. produces combinations of 0..19
# 2. takes about 0.9 secs
#
  restart;
  t1:=time():
  C := Iterator:-Combination(20,10):
  B:= [seq(convert(c,set), c in C)]:
  t2:=time()-t1;
  whattype(B);
  numelems(B);
  B[1];
  whattype(B[1]);

# Option 3
#
# Modifying the above to produce a list of sets
# but with combinations on 1..20, rather than
# 0..19
#
# 1. produces combinations of 1..20
# 2. takes about 1.4 secs
#
  restart;
  t1:=time():
  C := Iterator:-Combination(20,10):
  B:= [seq(convert(c+1,set), c in C)]:
  t2:=time()-t1;
  whattype(B);
  numelems(B);
  B[1];
  whattype(B[1]);

If one is happy with 10-element combos of 0..19 rather than 1..20, then the code

t1:=time():
C := Iterator:-Combination(20,10):
B:=[seq(c, c = C)]:
numelems(B);
t2:=time()-t1;

runs in about 0.4secs on my machine

I could be wrong but I think yor problm is related to a fundamental misunderstanding of the use of the double underscore '__'

The double undescore is a "typographic convention" allowing you to incorporate an essentially *meaningless* subscript. Use of this type of subscript does not make its associated variable, 'indexable' in any way, shape or form

This "inert" subscript was (probably) introduced into Maple simply because many quantities in physics, engineering etc happen to use a pure name (including a meaningless subscript), as a variable name. For example the permittivity of free space is conventionally written as ε0. The subscript "0" does indicate any kind of indexable quantity - it is just a notational convenience

Consider the trivial example

n:=1;
epsilon__n:=n;

You appear to expect that this will return epsilon__1=1 - but it won't, it will return epsilon__n=1.

When you need indexable quantities, use [], as in

n:=1;
epsilon[n]:=n;

which will return epsilon[1]=1

I'm the guy who

  1. solved the basic problem in your original code, and
  2. although I made it work, I pointed out that it was pretty inefficient in places

You then sent a PM asking for ways to improve these 'inefficiencies'. As a general rule, I don't respond to PMs from this forum, but on this occasion I did.

My suggestions probably wouldn't have speeded-up your original code very much, just made it a lot easier to read/understand/debug.

You claim " it has been pointed by one of the forum participants that my code is inefficient, but no concrete tips were given even though I reached to that person". The version I provided was more logical, a lot shorter, and easier to understand

In your subsequent two posts, you chose to ignore these suggestions completely and just extend the problem, using your original (hard-to-read, hard-to-understand) code, with the fix I provided to make it function

If you continue to use verbose, redundant crud when you have been told how to remove/improve it, don't be surprised if people start to ignore your questions

After fixing the issue with the 'k' variable, there is nothing tehnically wrong with your code - so why fix it?

Well, not a good idea to get into bad habits so

  1. If variables are completely independent of the loop index, don't assign them within the loop
  2. As a general rule, if you need multiple plots of the same type then use plot([list of plots], options), rather than multiple plot statements

Check out the attached which corrects some of the points mentioned above (and maybe a few other things as well)

ctmp_corr-2.mw

This is still a long way from "perfect" - sorry, but I got a bit bored fixing stuff. It could probably be made much neater and tidier

My original code works perfectly

However since you use 2D Math input

  1. You inserted an extra space between 'seq' and '(', which 2D math is interpreting as "implied multiplication"
  2. You inserted an extra space betwee 'fprintf' and '(' which 2D math is interpreting as another "implied multiplication"
  3. You remove the '\n' character from the format string so you wouldn't have obtained nice 2-column data anyway

Try the following: Just comment out the path to the file on my machine and uncomment the path to the file on your machine

Numerical2.mw

  1. I can't read your worksheet either - same error message
  2. I tried the procedure at the link you supplied, and it failed with a different error - namely, Error, (in XMLTools:-ParseString) XML document structures must start and end within the same entity. Don't know how to fix this.
  3. I assume that you have no backup?
  4. Check the directory where you had been working on this file, and check whether there is a file of the same name with extension .bak or .BAK. Maple has an autosave capability (assuming that you haven't turned it off!). The .bak file is normally deleted if/when your worksheet is correctly saved. If you worksheet wasn't saved correctly, then this .bak file may still exist. If it does still exist, try to restore it using File->Recent Documents->Restore Backup

You state that the functions r1(t),r2(t), q(t) are defined earlier

And without these definitions, the code which you did upload is guaranteed not to work

Condsider the problem of finding an error in code which is guaranteed not to work

The first six lines of your code are

with(LinearAlgebra):
x1:=t->-r1(t)*cos(Pi/2-q(t)/2):
y1:=t->-r1(t)*sin(Pi/2-q(t)/2):
z1:=t->0:
x2:=t->r2(t)*cos(Pi/2-q(t)/2):
y2:=t->-r2(t)*sin(Pi/2-q(t)/2):

Now where exactly are the functions r1(t), r2(t) and q(t) defined????

Or do you expect to be able to plot quantities which use unlnown functions in their definition???

I suspect that the fundamental problem is that use of declare() only affects the way functions are displayed, not how they need to be addressed, assigned, whatever. You "declared" (amongst others), the function 'u(x, n, q, t)' so that it subsequently displays as u. But remember it only displays as u - if you wou want to perform a substitution like

subs(u = epsilon*u1+u0, contTrans)

then you need ot use the full definition (ie u(x, n, q, t)), not just the way the variable is displayed

Attached is a workheet. The top half (basically the part with 2-D input) is the OP's original, downloaded from his/her dropbox link

The bottom half (1D math input - sorry that's just the way I think) is the attempt I made at resolving the original problem, as I posted earlier

Question-2.mw

 

try the relevant help page by typing ?do at the Maple prompt

Aternatively, you can access Maple/help, by clicking on Help on the toolbar, and the entering 'do' in the search box

Maple help is generally useful - and prevents people from posting really inane questions here

First 138 139 140 141 142 143 144 Last Page 140 of 207