Carl Love

Carl Love

28025 Reputation

25 Badges

12 years, 305 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are replies submitted by Carl Love

@tomleslie It's clear from the title that the OP wants to integrate with respect to time, not distance.

@Carl Love 

Here's another way that uses a feature of int that is added by loading the Units:-Standard package:

with(Units:-Standard): 
Int(10*Unit(m/s^2), t*Unit(s));
value(%);

The reason that I didn't mention this before is that the inert Int gets prettyprinted in an inelegant way, which you'll see when you execute the code. This inelegancy only affects the display, not the computation. Despite that, it seems the closest to what you were trying. Still, I'd use the Intat.

@acer 

The reason that I didn't use Typesetting is that the objects it produces are type function, not type symbol or name. Thus there are numerous contexts where they can't be used, such as a variable of integration; and there are other contexts where they silently behave in different and undocumented ways, such as a bound variable in solve.

@Johan159 

Yes:

phi:= `#mo("phi", mathcolor= black)`;

@Gillee Considering the extent of programming involved in this Answer and the degree of efficiency improvement attained, I think that it deserves a Vote up (thumb icon in upper right corner).

I realized that compiled code recognizes parity checks (::odd::even), and by using these as a replacement for mod 2, I reduced the total time to 7 seconds. Again, that's the total time using a single processor. It should be possible to parallelize this (e.g., rowwise), but I have yet to get it to work with Threads.

I also added comments that explain the arithmetic.

restart
:
BuildA_M:= proc(
    A::Array(datatype= integer[4]), M::Array(datatype= integer[4]),
    r::integer[4]
)::integer[2];
local 
    i0::integer[2], i::integer[2], j0::integer[2], j::integer[2],
    m::integer[2], t0::integer[2], t::integer[2],
    p::integer[2], #parity (1=even, 0=odd) of 1 bits in a word
    Z::integer[2] #count of 0s
;
    for i0 to r do for j0 to r do
        Z:= 0; 
        for t0 to r do
            p:= 1; i:= i0-1; j:= j0-1; t:= t0-1; m:= M[t0];
            while i*t <> -j*m do #i.e., while they're not both 0
                #The effect on the low-order bit of the operation i*t-j*m is
                #equivalent to that of the operation Xor(And(i,t), And(j,m)).         
                if (i*t - j*m)::odd then #low bit = 1
                    p:= 1-p #Switch parity of count of 1-bits. 
                fi;
                i:= i/2; t:= t/2; m:= m/2; j:= j/2 #integer divisions!
            od;
            if p=1 then Z:= Z+1 fi
        od;
        A[i0,j0]:= Z
    od od;
    0
end proc
:
BuildA:= Compiler:-Compile(BuildA_M):

LArip:= proc(n::posint)
local 
    r:= 2^n, 
    M:= Array(1..r, combinat:-randperm(r) -~ 1, datatype= integer[4]),
    A:= Array((1..r)$2, datatype= integer[4])      
;
    BuildA(A, M, r);
    (A, M)  
end proc 
:
(A,M):= CodeTools:-Usage(LArip(8));
memory used=287.77KiB, alloc change=0 bytes, 
cpu time=7.06s, real time=7.07s, gc time=0ns

 

@Carl Love My code above can handle your longer example just as well:

str:= "A": s1:= " some very long string here":
s2:= " it was%s 10 ": s3:= "another very long string": 
x:= 10:
str:= sprintf(cat(str, s||(1..3)), if x++=10 then "" else x:=9; " not" fi);
x;
  str := "A some very long string here it was 10 another very 
     long string"

                               11

 

@nm Appending [] to a list or set returns the underlying sequence, just like op. Unlike op, this only works for lists and sets. I know that I've seen this in the help somewhere. 

It's sometimes useful to append [] to an rtable. This is a completely different usage, somewhat akin to eval, and the end result is still an rtable.

@mmcdara You've done a good job learning to use evalindets subsindets in 1-2 days.

I'm not sure what you're asking for. The curve that you're plotting is a polynomial. Polynomials don't have asymptotes. So, do you want to modify your function so that it's not a polynomial?

@mmcdara You wrote:

  • you need to use 1@@0 here because you want to add legends to a list of "PLOT structure" in a single shot.

Hmm, after a few moments thought, yes, that's totally true. The reason I hesitated though is that thinking about it like that does not occur to me while I'm coding a "surgery" such as this: My mental focus is entirely on what needs to be changed, not on what needs to remain unchanged such as the PLOT(...).

My 1@@0 could be replaced by c-> (c, _rest). Then it'd be fairly close to what you had, and probably just as efficient.

@Zeineb As mentioned by VV in another thread, you should probably change the procedure header. In particular, the algebraic is not doing you any good, and it may even be a problem in some cases not yet seen. I suggest this header:

proc(
   f::procedure, a::realcons, b::realcons, N::And(posint, Not(1)),
   {print_table::truefalse:= false, print_err::truefalse:= false}
)

@Zeineb I said tab_err:= Vector.... You have tab_err:= array....

@DarkMath To me, a true functional is something akin to Maple's command: It takes a procedure (function) as its argument and returns a procedure. It's easy to do the same thing with your integrals. Then you could do away with the need to pass in the r and pass in the shift.

f:= g-> 
    local _r, r; 
    subs(__R= intat(g(_r), _r= __r), __r= r, __r-> __R)
:
f(sin);
       r-> -cos(r) 

f(sin)(r);
                            -cos(r)

f(sin)(r+eps);
                         -cos(r + eps)

 

@mmcdara 1@@0 evaluates to ()-> args. The 1 could be replaced by almost anything and it'd still work; the 1 just seems appropriate. The zeroeth iterated power of any function is the (multi-argument) identity function. 1 (or any number) can be considered a function in Maple that returns itself. Note that (f@@2)(x) returns f(f(x)) and generalize from there.

If that's not sufficient explanation, just ask again.

First 134 135 136 137 138 139 140 Last Page 136 of 708