Joe Riel

9660 Reputation

23 Badges

20 years, 3 days

MaplePrimes Activity


These are answers submitted by Joe Riel

Simpler is to use unix tools such as uniq.  However, here's a Maple approach (I haven't actually tested it):

count := proc(file :: string)
local cnt_consec, cnt_uniq, line, max_consec, prev;
    prev := NULL;
    cnt_consec := 0;
    cnt_uniq := 0;
    max_consec := 0;
    do
        line := readline(file);
        if line = prev then
            cnt_consec := cnt_consec+1;
        else
            prev := line;
            if cnt_consec > max_consec then
                max_consec := cnt_consec;
            end if;
            cnt_consec := 0;
            cnt_uniq := cnt_uniq+1;
            if line = 0 then break; end if;
        end if;
    end do;
    (cnt_uniq, max_consec);
end proc:

For the particular example you chose, all three use LinearAlgebra:-Diagonal, so you might as well call it directly. MTM:-diag is equal to ArrayTools:-Diagonal, which is a somewhat more general version of LinearAlgebra:-Diagonal, i.e. it applies to Arrays as well as Matrices and can be used to convert a Vector to a diagonal Matrix. MTM is a package for calling Matlab from Maple, some of the commands work without Matlab.

The OP asks when it is appropriate to use seq vs map.  For typical usage this is more a stylistic choice than one of efficiency.  Sometimes the choice depends on what you want to do with the result: if it has the same form as the "input" then map is natural.

Note that if you are using seq inside a procedure (my normal usage), then you should declare the index variable as a local. That isn't strictly required, however, if you don't there can occasionally be issues.  For example

(**) protect(k);
(**) proc() seq(k, k=1..3) end();
Error, (in unknown) attempting to assign to `k` which is protected.  Try declaring `local k`; see ?protect for details.

 

 

The most likely cause of this is that a parameter value was entered incorrectly. Try viewing the Modelica source for the model, it may be easy to spot it in the listing.

The way to do that is to convert the parameters to variables in the source.  A symbol declared as a parameter cannot change during a simulation, though its initial value can be computable.

Use collect with the factor transformation applied to the coefficients:

collect(a, mu(F,H), factor);

 

The real desire here, I think, is to keep intact 1/tau - tau.  One approach is to freeze, factor, and thaw:

ex := 1/(1/tau-tau):
thaw(map(factor, subs(ex = freeze(ex), Expr)));

I modified this; reread the post and realized the user wanted to print the output to a file.  This does that.

Compute := proc( data :: string, cols :: posint, ofile :: string )
local A, B, cnt, fd1, fd2, fmt, i, j, line, p, row, x;

    fd1 := fopen(data,'READ');
    fd2 := fopen(ofile, 'WRITE');
    fmt := StringTools:-Repeat("%f",cols);
    A := Matrix(0,1..cols); # working Matrix
    cnt := 0;               # number of Matrices read
    i := 0;                 # number of rows in current Matrix
    do
        line := readline(fd1);
        if line = 0 or line = "" or line[1] = "#" then
            if i > 0 then
                # Matrix was started; assume it is complete.
                cnt := cnt+1;
                # Specify the (possible) submatrix of A;
                B := A[1..i,..];
                # Compute charpoly and extract coefficients as Vector
                p := LinearAlgebra:-CharacteristicPolynomial(B,x);
                p := PolynomialTools:-CoefficientVector(p,x,'termorder=reverse');
                fprintf(fd2,"%{c,}a\n",p); # see ?rtable_printf for details
                i := 0;
            end if;
            if line = 0 then
                # end of file, exit loop
                break;
            end if;
        else
            # Parse row and append to Matrix
            i := i+1;
            row := sscanf(line, fmt);
            for j to cols do
                A(i,j) := row[j];
            end do;
        end if;
    end do;
    fclose(fd1,fd2);
    cnt; # return number of Matrices
end proc:
Compute("read-data-compute-charpoly.dat",3,"read-data-compute-charpoly.poly");

The resulting file contains

1,-15.,-18.,0
1,-6.,13.,-28.
1,-3.,1.,3.

 

That cannot be done.  What are you really trying to accomplish?  

Is this simplified version what you want?

with(VectorCalculus):
vars := [x,y]:
J := Jacobian([x^2*y, x+y], vars);
f := unapply(convert(J,listlist),vars);
CodeGeneration[C](f,optimize,declare = map(`::`,vars, float));
void f (double x, double y, double cgret[2][2])
{
  double t3;
  t3 = x * x;
  cgret[0][0] = 0.2e1 * x * y;
  cgret[0][1] = t3;
  cgret[1][0] = 1;
  cgret[1][1] = 1;
}
 

 

Use the following:

_LatexSmallFractionConstant := 0:
latex(1/2);
{\frac {1}{2}}

That depends somewhat on the form of n.  For example, if n is a numeric fraction, then this is readily solvable. Similarly if x and y are purely symbolic, so one merely has to identify the syntactic form. 

You could use eliminate with a dummy variable (I used the underscore):

eqs := {x-y=0, y-x=0}:
eliminate(eqs, _)[2];
                               {x-y}

Doing so converts the remaining equations to expressions implicitly equal to zero.  

 

The first consideration might be whether the original data should be in a list. An Array, possibly of datatype = float[8], could be a better choice. Depending on what f is, you might be able to use a compiled procedure. Or use evalhf. Also returning a list (from f), then using only one element of it is not ideal. 

Before doing anything, consider using a profiling tool (CodeTools[Profiling]) to determine where most of the time is being spent.  

Did you try the LongestCommonSubstring command in StringTools?

First 27 28 29 30 31 32 33 Last Page 29 of 114