Joe Riel

9530 Reputation

23 Badges

20 years, 24 days

MaplePrimes Activity


These are answers submitted by Joe Riel

Here's a modification to Roman's method that avoids O(n^2) list building.  If is useful if there are many repeated elements.

TableInverse := proc(T)local eq, indx, tmp, inv;
    for eq in op(2,op(T)) do tmp[rhs(eq)][lhs(eq)] := 0 end do;
    for indx in indices(tmp,'nolist') do inv[indx] := {indices(tmp[indx],'nolist')}; end do;
    op(inv);
end proc:


The Maple ?rsolve procedure solves this if you convert to polynomials. 

eqs := { a(n)*(a(n-1)+3) = a(n-1)+2, a(1) = 2 }:
rsolve(eqs, a(n) ); 

 

There are a few ways you can do this with Maple.  One is to mimic Matlab's multiple output method.  For example

MyProc := proc(x) return ( x, x+1, x^2 ); end proc:
(a,b,c) := MyProc(3); 
                            a, b, c := 3,4,9

The variables a, b, and c are now assigned 3, 4, and 9, respectively.

That isn't, however, necessarily the best approach, particularly if you are dealing with Matrices. You might be better off passing the output Matrices to the procedure as arguments and have the procedure operate on them directly.  For example

MyProc := proc(A :: Matrix, B::Matrix, C::Matrix) 
    B[..,..] := A^2;
    C[..,..] := A+B;
    NULL;
end proc:
X := <1,2;3,4>: Y := Matrix(2): Z := Matrix(2):
MyProc(X,Y,Z):
Y,Z;
          [ 7  10]   [8  12]
          [     ],  [     ]
          [15 22]  [18 26]
y := 16^j/16:
subs(16=z,1/16=1/z,y):
combine(%,power,symbolic):
subs(z=16, %);
 

I now see that that is essentially Carl's approach, but with ?combine,power instead of simplify. Hmm. A simpler approach, then, is

y := 16^j/16:
subs(k=j-1,simplify(subs(j=k+1,y))); 

 

{DE,DF} is a set of lists.  To get a set of equations, do op~({DE,DF}).

Computing manually I get 30!/25!/3!/2!*5^2 = 35626500. This comes from creating the venn diagram of the sets, noting that there must be exactly 3 in the middle region, 25 in C minus A minus B, leaving 2 for the other 5 regions.  Those two can be distributed 5^2 ways.  The lhs of the product is the number of ways to partition a set of 30 elements in subsets of 25, 3, and 2.

Note: fixed original computation

Per response from OP, my assumption that A union B union C = S is incorrect. Fortunately, it is easy to handle the more general case, just multiply by the number of ways to partition a set of 25 elements into two sets (the sets being C \ (A union B) and S \ (A union B union C)). That factor is 2^25.  So the overall result is

30!/25!/3!/2!*5^2*2^25 = 1195426971648000

Another option, possibly simpler, is to use a DataTable embedded component.  From the Components palette, click the DataTable icon.  A dialog will open querying for the size and name of the associated Matrix. You can directly access and assign elements in the DataTable via the associated Matrix.  You can also change the row and column names by right-clicking on the embedded component and selecting the "Row Name" or "Column Name" items. The names are for display, access to the cells is via integer indices.

:: is the type testing operator (at least in this context).  'w' is not a type, unless have created a type w, which can be done with ?TypeTools or by assigning to `type/w` (the former method is preferred). Hmm. Maybe the issue you are having is that the second if function has forward-quotes around it rather than back-quotes, as the first does.  Back-quotes are needed.

This is doable, at least on Linux, I'm not sure about Windows. However, it may not be the best way to go.  Rather than using the Optimization template directly, I'd consider extracting the code you need from it to do whatever computations are necessary, putting them in a Maple script, and calling it from command line Maple (cmaple in Windows).  That will be faster and cleaner since you won't have to interface with the GUI.  

To execute command line maple from a script, use

cmaple some_maple_script.mpl

Of course, cmaple needs to be in the PATH. 

The xml was missing some tags.  Not sure I restored it properly, but it opens now and is usable. 

Optimal_Portfolio_C.mw

It would be helpful to upload the worksheet (green arrow), or paste the code into a text region so it can be copied. Note that you should probably have used Pi rather than pi.

Connect the revolute joint to the tip of a rigid body frame.  Change the orientation of the tip of that frame, either by using Euler angles or a rotation Matrix.

There are several ways to handle this. A useful technique is to separate the grading data from the algorithm that computes the grade. Here is one way to do that---a bit overkill for this task, but maybe it will give you some ideas.  An Array of records stores the grading data.  Note, by the way, that there is a bug in your routine: try entering 100.

Marks := module()
export ModuleApply;
local  Grades;

    Grades := Array(1..5
                    , [ NULL
                        , Record['packed']('upperbound' = 40,  'grade' = "Fail")
                        , Record['packed']('upperbound' = 50,  'grade' = "Third Class")
                        , Record['packed']('upperbound' = 60,  'grade' = "Lower Second Class")
                        , Record['packed']('upperbound' = 70,  'grade' = "Upper Second Class")
                        , Record['packed']('upperbound' = 100, 'grade' = "First Class")
                      ]);

    ModuleApply := proc(mark :: positive)
    local i,rec,threshold;

        for i to upperbound(Grades) do
            rec := Grades[i];
            threshold := rec:-upperbound;
            if mark < threshold then
                printf("%s%s: %a\n"
                       , rec:-grade
                       , `if`(threshold-mark < 3
                              , " (borderline)"
                              , ""
                             )
                       , mark
                      );
                return NULL;
            end if;
        end do;
        printf("Obviously cheating: %a\n", mark);
    end proc;

end module:

Another issue is that the declaration of the parameters is wrong.  As given, only parameter c is type-checked.  You want

proc( a :: positive, b :: positive, c :: positive ) ... end proc

Note that this declaration won't allow, say, IsTriange(1,2,Pi), since Pi is not considered numeric; whether you want to allow that is up to you.  If you do, you might declare them as type realcons, then inside the procedure convert them to floats with evalf, check that they are positive, then check that the longest is shorter than the sum of the other two.

Maple's relation operators are nonassociative, so your condition is not syntactically correct. You could express that as 0 < m and m <= 39.99, however, that is neither necessary nor desirable.  Well, probably not desirable.  Despite the given table, you are better off considering any value less than 40 as a fail.  Otherwise a value such as 39.995 leads to an indeterminate condition.  It could be handled as an error, but that seems a poor choice.  

I'd so something like

Grade := proc(mark :: nonnegative)
   if mark < 40 then return (mark, "fail");
   elif mark < 50 then return (mark "3rd");
   ...
  end if;
end proc:

Note that the single argument is declared as nonnegative.  Consequently, calling it with, say, Grade("hello") will return an appropriate error message. 

First 32 33 34 35 36 37 38 Last Page 34 of 114