acer

32333 Reputation

29 Badges

19 years, 319 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

It's not clear whether you want to augment with all zeroes, or with specific data.

If you just want to create a larger Matrix whose entries in rows/columns 1..4 are the original M, then it is easy to do it in several ways. Here are two:

restart:
with(LinearAlgebra):
M:=RandomMatrix(4,4);

N:=copy(M):
N(7,7):=0:
N;

restart:
with(LinearAlgebra):
M:=RandomMatrix(4,4);

N:=Matrix(7,7,M);

Of course, you could easily modify either of those so that the result was M, and not N.

acer

The ArrayTools:-Copy command can be used to assign to the diagonal of a rectangular storage Matrix efficiently.

M:=LinearAlgebra:-RandomMatrix(3,2);

                                    [ 13  92]
                                    [       ]
                               M := [-51  59]
                                    [       ]
                                    [-65  20]

V:=LinearAlgebra:-RandomVector[row](2); # or column Vector

                                V := [-48, 0]

# Acts on newM. Use M if you want M overwritten.
newM:=copy(M):

ArrayTools:-Copy(min(3,2),V,newM,0,3+1);

newM, M;

                            [-48  92]  [ 13  92]
                            [       ]  [       ]
                            [-51   0], [-51  59]
                            [       ]  [       ]
                            [-65  20]  [-65  20]

You can also create a procedure to do it. Here's one version, with boilerplate to try and make it robust for mixed datatypes and storage orders.

replacediag := proc( A::Matrix(storage=rectangular),
                     B::Vector(storage=rectangular),
                     {inplace::truefalse:=false} )
local Anew, Bnew, m, n;
  if inplace then Anew := A; else Anew := copy(A); end if;
  if rtable_options(B,':-datatype')<>rtable_options(A,':-datatype') then
    Bnew := Vector(B,':-datatype'=rtable_options(A,':-datatype'));
  else Bnew := B;
  end if;
  (m,n) := LinearAlgebra:-Dimensions(Anew);
  ArrayTools:-Copy(min(m,n), Bnew, Anew, 0,
    `if`(rtable_options(Anew,':-order')=':-Fortran_order', m+1, n+1));
  Anew;
end proc:


replacediag(M,V), M; # M is not overwritten. Result is a new Matrix.

                            [-48  92]  [ 13  92]
                            [       ]  [       ]
                            [-51   0], [-51  59]
                            [       ]  [       ]
                            [-65  20]  [-65  20]

replacediag(M,V,inplace), M; # M is overwritten. This saves time/memory.

                            [-48  92]  [-48  92]
                            [       ]  [       ]
                            [-51   0], [-51   0]
                            [       ]  [       ]
                            [-65  20]  [-65  20]

acer

I did something like that here, embedding "plot" axes onto an image.

The basic idea was to produce an empty plot containing just the axes (or in your case, a textplot without axes perhaps), export that as an image, read in that image using ImageTools, and finally overlay it onto your other (background) image. The overlaying is done just by overwriting the Array entries -- copying the overlay image's Array's nonzero entries onto the background image's Array.

There was some trickiness with adjusting for whitespace border around the exported plot. The referenced link might help with that.

acer

I changed val_M0 to vec_M0 (which you probably had in your original, a transcription error, perhaps).

That allowed the posted code to produce the nonreal result,

fsolve({mat_M_V[2,1]=0,mat_M_V[1,2]=0,Im(mat_M_V[1,1])=0,Im(mat_M_V[2,2])=0},
       {alpha11=0,alpha22=0,alpha12=0,theta=0},'fulldigits');

           {theta = -1.666216272 + 0.4818505195 I, alpha11 = -1.570796327, 

            alpha12 = -9.424777961, alpha22 = 3.141592654}

So then I specified a real range for theta, and got (with or without option 'fulldigits'),

fsolve({mat_M_V[2,1]=0,mat_M_V[1,2]=0,Im(mat_M_V[1,1])=0,Im(mat_M_V[2,2])=0},
       {alpha11=0,alpha12=0,alpha22=0,theta=0},theta=-infinity..infinity);

             {theta = 3.997794044, alpha11 = -1.570796327, 

              alpha12 = 1.570796327, alpha22 = -1.570796327}

Interestingly, 1.570796327 = Pi/2, which makes me wonder whether some exact approach is also possible (before assigning float values to parameters).

acer

This is an important question. It's actually one of the most basic instances of a more general set of computational tasks, for which dsolve/numeric has been augmented with a whole slew of facilities. The functionality is known as Events.

The basic notion is like this: a rootfinder (like fsolve, etc) operating on the result from a call to dsolve/numeric is acting a bit in the dark. It needs to be told special information about the dependent functions (like x(t) or y(t) say) in order to get the best chance of finding the desired root. The seperate rootfinder doesn't even know that the solution exists (which can be helpful knowlegde). It doesn't know the range, or the stiffness, etc, that characterizes the curve.  But dsolve/numeric itself is computing the solved approximations to the dependent functions, so it is in the best position to be able to invert that computation.

In practice, using dsolve/numeric with "events" can get such inversion results using less resources (memory and time), and quite often more accurately.

The accuracy issue is subtle. The accuracy of the dsolve/numeric solution is (generally) only as good as what is specified by its abserr and relerr tolerances. Suppose that you obtain a procedure from dsolve/numeric, which computes y(t) for any given value of `t`. Bumping up Digits, so as to try the separate rootfinding (eg. fsolve) to obtain a high accuracy value of the independent variable `t` that satisfies y(t)=K for a given numeric value K, is not going to get you a more accurate answer. The rootfinding results are only as good as the interpolating spline that dsolve/numeric uses to compute y(t) in the first place.

So (I feel that) there is some risk in using a separate rootfinder like fsolve, of being beguiled into believing that one has control over the accuracy of the computed roots.

It's a little more work to set up, for involved examples, but it can pay off for larger and more difficult or computationally intensive projects.

Here is an illustration, using Markiyan's example. I've also used output=listprocedure, because that makes subsequent computation of x(t) and y(t) easier (without needing `op`).

restart:

sys := (D(x))(t) = y(t)-x(t), (D(y))(t) = x(t)*(D(x))(t)-2*y(t):
ic := x(0) = 1, y(0) = -1:

# Default for nonstiff rfk45 is abserr=1e-7, relerr=1e-6
sol := dsolve({ic, sys}, numeric
              , parameters=[Py]
              , events=[[y(t)-Py,halt]]
              , output=listprocedure):

solt:=eval(t,sol):
solx:=eval(x(t),sol):
soly:=eval(y(t),sol):

interface(warnlevel=0):

solt(parameters=[-0.5]):
ans := CodeTools:-Usage( solt(1000) );

memory used=213.85KiB, alloc change=0 bytes, cpu time=15.00ms, real time=12.00ms
                          ans := 0.623905007259310

soly(ans);

                             -0.500000000000000

restart:

sys := (D(x))(t) = y(t)-x(t), (D(y))(t) = x(t)*(D(x))(t)-2*y(t):
ic := x(0) = 1, y(0) = -1:

# Default for nonstiff rfk45 is abserr=1e-7, relerr=1e-6
sol := dsolve({ic, sys}, numeric
              , parameters=[Px,Py]
              , events=[[x(t)-Px,halt],[y(t)-Py,halt]]
              , output=listprocedure):

solt:=eval(t,sol):
solx:=eval(x(t),sol):
soly:=eval(y(t),sol):

interface(warnlevel=0):

solt(parameters=[1e10, -0.5]):

ans := CodeTools:-Usage( solt(1000) );

memory used=216.88KiB, alloc change=127.98KiB, cpu time=0ns, real time=12.00ms
                          ans := 0.623905007259310

soly(ans);

                             -0.500000000000000

solt(parameters=[0.95, 1e10]):
ans := CodeTools:-Usage( solt(1000) );

memory used=31.67KiB, alloc change=0 bytes, cpu time=0ns, real time=2.00ms
                          ans := 0.0253259108029954

solx(ans);

                              0.950000000000000

Here's a worksheet, comparing using Events and fsolve for this example. Note both memory allocation as well as speed differences. This is just expository and, being based on a single example, is not supposed to prove anything. For one's own serious projects, it'd be best to try out the alternatives.

dsolveinv0.mw

acer

Have you tried CodeGeneration[Matlab] ? That will work on some Maple procedures or expressions (but not on entire Documents or Worksteets).

acer

I have seen it done on the same host, using the commandline interface. That is, starting the kernel and the client (interface) separately.

I don't know whether it could be done across a network. I'll give it a try (later, sorry).

By "X-window interface" I presume that you mean the Classic GUI. If anything, I would expect that the Standard (Java) GUI would be more difficult than Classic here, as far as accepting parameters when launched and not automatically firing up its own kernel.

acer

I found an ubuntu 12.04 machine and reproduced the problem in the commandline interface. I will look harder, and submit a bug report. You don't need to fiddle with libname and other stuff that I suggested earlier (now deleted).

%maple15.01 -s
    |\^/|     Maple 15 (X86 64 LINUX)
._|\|   |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2011
 \  MAPLE  /  All rights reserved. Maple is a trademark of
   Waterloo Maple Inc.
      |       Type ? for help.

> kernelopts(version);           
            Maple 15.01, X86 64 LINUX, Jun 1 2011, Build ID 635833

> ssystem(`more /etc/*release*`);
[0, "DISTRIB_ID=Ubuntu
    DISTRIB_RELEASE=12.04
    DISTRIB_CODENAME=precise
    DISTRIB_DESCRIPTION="Ubuntu 12.04 LTS"

    "]

> int(x^n, x=2..3);
                                     3
                                    /
                                   |    n
                                   |   x  dx
                                   |
                                  /
                                    2

I strongly suspect that the underlyng problem is another issue which has been previously reported. On that same system,

> log[2](8);
Error, (in iroot) powering may produce overflow

It's quite plausible that `int` is generating that error internally, but catching and suppressing it, with the end result being that it returns unevaluated.

Here is a comment to a previous report, suggesting that you contact Maplesoft's Tech Support ( support@maplesoft.com )

Also, the example succeeds in 64bit Maple 16.00 on that same machine.

acer

M:=LinearAlgebra:-RandomMatrix(5,generator=0..2);

                                 [0  0  1  1  1]
                                 [             ]
                                 [2  0  1  0  0]
                                 [             ]
                            M := [2  1  2  2  0]
                                 [             ]
                                 [1  0  2  0  2]
                                 [             ]
                                 [0  1  0  0  1]

rtable_num_elems(M,'NonZero');

                                     14

add(`if`(x<>0,1,0),x=M);

                                     14

You can subtract the above numbers from the total number of entries (the product of dimensions, 5*5 or 18*18, etc) to get the number of zeros.

acer

This is not short and pretty,

> expr := -2*Pi*sin(Pi*a)/(-1+cos(2*Pi*a)):

> combine(simplify(convert(convert(expand(expr),tan),sincos)));

                                      Pi
                                   ---------
                                   sin(Pi a)

acer

You can plot anything you want. Whether it is "correct" depends on what you are trying to represent.

The fourier transform takes the representation in the time domain of some signal and represents it instead in the frequency domain. The frequency domain representation consists of amplitude and phase, which are the real and imaginary parts respectively. One reason that (Re(f)^2+Im(f)^2) is often considered is that it is equal to f*conjugate(f). These quantities have special names (such as spectral density) and are directly related to the power of the signal.

There are lots of wikipedia pages for this kind of thing. You could look at spectral density, for example. Then maybe move back to the simpler frequency spectrum and frequency domain.

Don't confuse amplitude with magnitude.

acer

You have a space (blank character) between the Im and the (b[i]) in your conditional test. You are using 2D Math input and so the subexpression containing that extra space gets interpreted as an implicit multiplication. So, after parsing, part of the conditional is the test Im*b[i] <> 0 which is true for all your data. If it weren't for this syntax mistake then your code might do what you want.

When you write "row" in your question, it seems that you mean column. No problem.

Your method is not very efficient, since insertion into lists by assigning to specific list entries is not efficient. (It recreates whole new lists, as lists are really a mutable data structure in Maple -- they are just faked to appear so, when of length less than 100.) You could also accomplish the task by building up the new lists in one go, using the seq command. But for only small amounts of data perhaps you don't need to be so concerned with the efficiency.

acer

You can create your own procedure to put this information into a format that you might find useful.

As just one example (of many possible),

query:=proc(z::rtable)
          op(0,z), [op(`if`(op(0,z)=Array,2,1),z)];
       end proc:

M:=Matrix(3,4):

V:=Vector[column](5):

A:=Array(1..2,-1..1):

t,d := query(M);

                                  t, d := Matrix, [3, 4]

t,d := query(V);

                               t, d := Vector[column], [5]

t,d := query(A);

                            t, d := Array, [1 .. 2, -1 .. 1]

acer

You could look at Parabolic Reflectors and the Ideal Flashlight.

He compares a parabola with a circle, as reflectors. You could try and extend to 3D.

acer

If you execute the Maple command,

libname;

then you should be able to figure out where the `lib` folder resides.

If you don't want to mess around with the folder belonging to your installed Maple then you could try placing them in a folder which you place as follows: First, find out what Maple considers to be your home folder, by executing the Maple command,

kernelopts(homedir);

Append the result of that with something like /maple/toolbox/Trig/lib" and create those folders (relative to whatever kernelopts(homedir) reported, naturally).

In any case, once the two files are copied into location, close and relaunch Maple.

You would need both of your files Trig.lib and Trig.ind as they form a Maple archive together. (In modern Maple such an archive would usually created as a single file named Trig.mla but whoever wrote your pair is doing it the old way.)

acer

First 264 265 266 267 268 269 270 Last Page 266 of 336