acer

32363 Reputation

29 Badges

19 years, 332 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are replies submitted by acer

@Thomas Dean See also the `unapply` example above, at end.

So you can indeed get your methodology to work. But if you cannot remember why you need `unapply`, and when, then perhaps you might reconsider the whole approach and try to stick to expressions or procedures, only, as much as possible... Just a suggestion.

I note that confusion between functions and expressions is #1 on Robert Israel's Top Ten list. Worth a read.

One pattern I've observed is people creating a procedure like your original `f`, only to ever call it once as `f(x)`. If it's only ever called once, just to get an expression, then the odds seem good that there is a procedure/expression muddle of some sort.

Why not just assign the values into a Matrix or Array and then use ExcelTools:-Export?

I don't see why printing all the values would be useful or convenient for exporting, or anything but very inefficient.

acer

@Kitonum When the two points lie on adjacent faces you could consider three kinds of flattened cross. One cross has as its center square the face containing point A. The other two crosses can have as their center squares respectively one of the two faces which are adjacent to both the face containing A and the face containing B. For each of these three kinds of cross take the straight line distance between A and B. Will the answer for this case (A and B lying on adjacent faces) then be the minimal length of the three straight line segments formed on each of these three flattened crosses?

 

@Markiyan Hirnyk There is also time taken by RandomArray to first produce M.

On my 64bit Maple 2015.1 on Windows I see timings that on average look like so,

restart:
CodeTools:-Usage(map[evalhf,inplace](c->.1*c+2,
                                     ArrayTools:-RandomArray(1000,1000,distribution=normal))):
memory used=61.71MiB, alloc change=7.63MiB, cpu time=998.00ms, real time=994.00ms, gc time=62.40ms

restart:
CodeTools:-Usage(Statistics:-Sample(Statistics:-RandomVariable(Normal(2,.1)),[1000,1000])):
memory used=8.16MiB, alloc change=7.63MiB, cpu time=125.00ms, real time=134.00ms, gc time=0ns

It's also worth pointing out that the OP asked about vectors and matrices. The RandomArray command produces Arrays, while Sample can produce Vectors and Matrices directly. Sure, a 1D and 2D Array can be toggled inplace into a Vector and a Matrix using rtable_options(...,subtype=...) but it makes the task more complicated.

The code using Sample is more straightforward, and even more so than above if one wants to reuse the random variable or generate a numeric sample more than once.

FWIW, the help-page for RandomArray says that it produces matrices/Matrices when called like RandomArray(m,n,distribution=normal) but that is not true. I see RandomArray(m,n,distribution=normal) make a 2D Array, and RandomArray(m,distribution=normal) make a Matrix, and no option to make a 1D Array or a Vector or a nonsquare Matrix directly.

@Kitonum Until the OP responds we don't for sure whether he just wants a random sample from some Normal(a,b), or whether he wants a Matrix of random variable placeholders.

Here is one (slightly crazy) way to produce Matrix of sample values from that.

M := Matrix(2,3,(i, j)->Statistics:-RandomVariable(Normal(0,1))):

map(`?[]`,map(Statistics:-Sample,M,1),[1]);

         [-0.531692519958605  1.99932940923732   0.105259473657208]
         [                                                        ]
         [ 0.127790495266725  1.37115530221358  0.0925191256937789]

Slightly less crazy might be,

map(c->Statistics:-Sample(c,1)[1],M);

          [0.342696098518556  0.837118218825433  -1.10381342439977]
          [                                                       ]
          [ 1.39898388585646   1.87933504556442  0.477078415677388]

@nm Well, that free MaplePlayer already exists, and your proposed way of doing things is already possible (and, I agree, results in a more responsive and robust experience). And that free player also provides direct access to download directly from the Maple Cloud.

 

@erik10 Sorry to belabour it, but just to make sure:

1) You can share private access to a Maple Could group with 1 or more other people. So once that is done you no longer need to send them URLs -- each party knows where to deposit or find something new.

2) The material on the MapleCloud can be downloaded from a brower and then opened in the MaplePlayer. Or it can be accessed from the cloud and opened directly from within the Player. Or it can be accessed and opened and interacted with in a (html5 conforming) browser, without the Player. And if you login at maplecloud.maplesoft.com then you get access to your own private groups, in your browser.

The browser support for all interactive aspect of a Document (via Embedded Components) seems to be still being polished. But it seems to me that point 2) speaks to your original Question.

@lemelinm It's up to you, I'd say. If you use poundforce then you might get asked to define a slug.

1 lbf = 1 slug*ft/s^2

while,

1 pdl = 1 lb*ft/s^2

I'm not 100% sure which is used more regularly, in the USA, but I've seen indications that they might use lbf ie. pound-force. (I live in a country that uses kg and N.)

We can also make wrapper around DirectSearch:-SolveEquations which rejects spurious solutions which do not meet the residual tolerance.

Not surprisingly it can detect (depending on the specified tolerance) similar inaccurate candidate solutions that fsolve baulks (with all defaults as before).


restart:

VP := Vector[row](16, {(1) = 10, (2) = 177.9780267, (3) = 355.9560534,
                       (4) = 533.9340801, (5) = 711.9121068, (6) = 889.8901335,
                       (7) = 1067.868160, (8) = 1245.846187, (9) = 1423.824214,
                       (10) = 1601.802240, (11) = 1779.780267, (12) = 1957.758294,
                       (13) = 2135.736320, (14) = 2313.714347, (15) = 2491.692374,
                       (16) = 2669.670400}):

VE := Vector[row](16, {(1) = 5.444193931, (2) = .4793595141, (3) = .3166653569,
                       (4) = .2522053489, (5) = .2123038784, (6) = .1822258228,
                       (7) = .1544240625, (8) = .1277082078, (9) = .1055351619,
                       (10) = 0.8639065510e-1, (11) = 0.6936612570e-1,
                       (12) = 0.5388339810e-1, (13) = 0.3955702170e-1,
                       (14) = 0.2612014630e-1, (15) = 0.1338216460e-1,
                       (16) = 0.1203297900e-2}):

oldDigits,Digits := Digits,10:
forget(evalf);
for i to 15 do
  p[i] := VE[i+1] < x and x <= VE[i],
          (VP[i+1]-VP[i])*(x-VE[i])/(VE[i+1]-VE[i])+VP[i] end do:
Digits := oldDigits:
forget(evalf);

g := unapply(piecewise(seq(p[i], i = 1 .. 15)), x):

for i to 15 do
  forget(evalf);
  print(proc(j) local res;
                res:=fsolve(g(x) = VP[j]);
                if res::numeric then res else j end if;
        end proc(i));
end do;
forget(evalf);

                                 5.444193930
                                0.4793595141
                                0.3166653569
                                      4
                                0.2123038784
                                0.1822258228
                                      7
                                0.1277082078
                                      9
                                0.08639065507
                                     11
                                0.05388339812
                                0.03955702173
                                     14
                                0.01338216464

StrictSE:=proc({tols::[numeric,numeric]:=[1e-6,1e-6]} )
  local res;
  res:=DirectSearch:-SolveEquations( _rest,
                     ':-tolerances'=tols );
  if max(map(abs,res[2]))<=tols[2] then
    res;
  else
    NULL;
  end if;
end proc:

for i to 15 do
  forget(evalf);
  print(proc(j) local res;
                res:=StrictSE(g(x) = VP[j], tols=[1e-10,1e-7]);
                if res::list then eval(x,res[3]) else j end if;
        end proc(i));
end do;
forget(evalf);

                              5.44419392993762
                              0.479359514097377
                              0.316665356905805
                                      4
                              0.212303878414364
                              0.182225822810105
                                      7
                              0.127708207765792
                                      9
                             0.0863906550747353
                                     11
                             0.0538833981179015
                             0.0395570217241483
                                     14
                             0.0133821646309684

for i to 15 do
  forget(evalf);
  print(proc(j) local res;
                res:=StrictSE(g(x) = VP[j], tols=[1e-14,1e-10]);
                if res::list then eval(x,res[3]) else j end if;
        end proc(i));
end do;
forget(evalf);

                              5.44419392993762
                                      2
                              0.316665356908418
                                      4
                              0.212303878418517
                              0.182225822750651
                                      7
                              0.127708207763479
                                      9
                             0.0863906550708061
                                     11
                             0.0538833981170618
                             0.0395570217254968
                                     14
                             0.0133821646366919

 


Download DSrej.mw

@Markiyan Hirnyk The DirectSearch:-SolveEquations command accepts a tolerance option similar to that of DirectSearch:-Search.

   tolerances =[positive, positive], [positive], or positive -- Set the absolute
   tolerances for the extremum point (tolerances[1]) and minimum (or maximum) value
   (tolerances[2]). The search is stoped when the following exit conditions are met
   several times:  ||Xmin-Xold ||<=tolerances[1] and |fmin-fold |<=tolerances[2].
   The default value is [10^(-6), 10^(-6)]. If tolerances contains one value
   this value corresponds to both the extremum point and minimum (or maximum) value.

Note that the help-page for DirectSearch:-SolveEquations also says, as a last Note before its Examples, "...If the residuals too large then the solution is not exact solution but it is only solution that minimizes the residuals."

That last sentence is the key. It surely means that SolveEquations is going to return a result even if it does not meet the criteria of being a root. And that is just what it does. Let's call DirectSearch:-SolveEquations and specify tolerances.

DirectSearch:-SolveEquations( g(x)=VP[14], tolerances=[1e-10,1e-10] );

    [                   -16  [                    -8]                               ]
    [2.24381757423686 10   , [-1.49793777382001 10  ], [x = 0.0261201463011024], 40 ]

The middle value -1.49793777382001e-8 is documented on that help-page as meaning the residual. (The first value in the returned result is just the square of that, because, as documented, the objective used is f[1](x)^2 by default.) So the returned result does not meet the second passed tolerence value which specifies the acceptance target for the residual.

So in the sense that DirectSearch:-SolveEquations considers the matter, the returned value is not a (exact, its word) root. But the result is returned anyway, as per that cited Note.

It's not returning what it considers to be a root. It's returning a value that minimizes the sum of the squares of the residuals. The absolute value of the residual (what I call the forward error) exceeds the tolerance supplied for the residuals, so it is not deemed to be an actual root.

@erik10 I am submitting a bug report that there are no cross-references in the help-pages plots,polyhedraplot and plots,polyhedra_supported to the help-page geom3d,polyhedra .

Alternatively, you could pay closer attention to what Kitonum wrote [implying under Digits=10 or higher, and correct at the default Digits=10 of your sheet], "Because  x= 0.02612014634  does not belong to  0.01338216460 < x and x <= 0.02612014630."

Note that increasing working precision to higher than Digits=10 will not make 0.02612014634 <= 0.02612014630, although lowering it will.

restart:
Digits := 10:
evalb( 0.02612014634 <= 0.02612014630 );
                                    false

restart:
Digits := 20:
evalb( 0.02612014634 <= 0.02612014630 );
                                    false

restart:
Digits := 9:
evalb( 0.02612014634 <= 0.02612014630 );
                                    true

@Markiyan Hirnyk The expression p[14][2] is not the part of the piecewise that matters when x=0.02612014634, when Digits=10.

When you call g(0.02612014634) the piecewise takes p[13][2] as the formula that gets computed, if g has been created with Digits=10 and if that is still the current working precision.



restart:

VP := Vector[row](16, {(1) = 10, (2) = 177.9780267, (3) = 355.9560534,
                       (4) = 533.9340801, (5) = 711.9121068, (6) = 889.8901335,
                       (7) = 1067.868160, (8) = 1245.846187, (9) = 1423.824214,
                       (10) = 1601.802240, (11) = 1779.780267, (12) = 1957.758294,
                       (13) = 2135.736320, (14) = 2313.714347, (15) = 2491.692374,
                       (16) = 2669.670400}):

VE := Vector[row](16, {(1) = 5.444193931, (2) = .4793595141, (3) = .3166653569,
                       (4) = .2522053489, (5) = .2123038784, (6) = .1822258228,
                       (7) = .1544240625, (8) = .1277082078, (9) = .1055351619,
                       (10) = 0.8639065510e-1, (11) = 0.6936612570e-1,
                       (12) = 0.5388339810e-1, (13) = 0.3955702170e-1,
                       (14) = 0.2612014630e-1, (15) = 0.1338216460e-1,
                       (16) = 0.1203297900e-2}):

oldDigits,Digits := Digits,10:
forget(evalf);
for i to 15 do
  p[i] := VE[i+1] < x and x <= VE[i],
          subs(x=xx,(VP[i+1]-VP[i])*(x-VE[i])/(VE[i+1]-VE[i])+VP[i]) end do:
Digits := oldDigits:
forget(evalf);

g := unapply(piecewise(seq(p[i], i = 1 .. 15)), x):

g(0.02612014634);

                        -13245.49210 xx + 2659.688538

p[14][2];

                        -13972.23133 xx + 2678.671074

p[13][2];

                        -13245.49210 xx + 2659.688538

forget(evalf):
trace(g):
oldDigits,Digits:=Digits,9;
g(0.02612014634);
untrace(g):
forget(evalf);
Digits:=oldDigits;

                         oldDigits, Digits := 10, 9

{--> enter g, args = 0.2612014634e-1

                        -13972.23133 xx + 2678.671074

<-- exit g (now at top level) = -13972.23133*xx+2678.671074}

                        -13972.23133 xx + 2678.671074
                                Digits := 10

forget(evalf):
trace(g):
oldDigits,Digits:=Digits,10;
g(0.02612014634);
untrace(g):
forget(evalf);
Digits:=oldDigits;

                         oldDigits, Digits := 10, 10

{--> enter g, args = 0.2612014634e-1

                        -13245.49210 xx + 2659.688538

<-- exit g (now at top level) = -13245.49210*xx+2659.688538}

                        -13245.49210 xx + 2659.688538
                                Digits := 10

 



Download fsolvexx.mw

@madagascar Please upload your actual worksheet (big green arrow in the editor here) in a new Comment to your Question.

Right now your post consists on just images of statements, and few if any people would want to retype that all by hand.

You'll need to make that lowercase display instead of Display.

Maple is a case-sensitive language.

acer

First 326 327 328 329 330 331 332 Last Page 328 of 592