How can I implement u^k = M^k u_0? - maple

Let M := Matrix([[0.94, 0.09], [0.06, 0.91]]); and u[0] = Vector([0.8, 0.2]);. What command could I use to get u^k = M^k u_0 for k=5, 10, ..., 50?

I'm guessing you mistyped, and you want to call the result of M^k . u[0] as u[k] rather than (as you wrote it) u^k.
restart;
with(LinearAlgebra):
M := Matrix([[0.94, 0.09], [0.06, 0.91]]):
u[0] := Vector([0.8, 0.2]):
# One (inefficient) way
seq(M^k . u[0],k=5..50,5);
# Another way
M5 := M^5:
for k from 5 to 50 by 5 do
u[k] := M5 . u[k-5];
end do;
# Another way
(evals,evecs) := simplify([Eigenvectors(M)])[]:
seq(evecs . DiagonalMatrix(map(`^`,evals,k))
. evecs^(-1).u[0],
k=5..50,5);

Related

Calculation of the number of microseconds in Ada

in C langage we have get_usec() which gives us the number of microseconds since the start of the current second.
-Speaking of the "current second" necessarily refers to time reference which is often EpochTime.
-In Ada.Calendar package, I see Seconds or Clocks functions by example with ability to split & get the seconds.
But how to get the number of microseconds since the start of the current second, please?
Thanks
Mark
Note that Ada.Calendar is for local time, and may jump backwards. If it's available (are there any post-83 compilers that don't provide it?), you'll be better off using Ada.Real_Time ARM D.8:
Now : constant Ada.Real_Time.Time := Ada.Real_Time.Clock;
Count : Ada.Real_Time.Seconds_Count;
Sub : Ada.Real_Time.Time_Span;
...
Ada.Real_Time.Split (T => Now, SC => Count, TS => Sub);
Now Count contains the number of whole seconds since the epoch and Sub contains the fraction of a second in addition to Count. Ada.Real_Time.To_Duration converts a Time_Span to Duration, allowing you to multiply it by 1E6 to get microseconds.
The packages Ada.Calendar and Ada.Calendar.Formatting provide the information you will need.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;
procedure Main is
Now : Time := Clock;
Seconds : Second_Duration := Sub_Second (Now);
begin
Put_Line
("Sub seconds since current second: " &
Second_Duration'Image (Seconds));
end Main;
The output of one execution of this program is:
Sub seconds since current second: 0.655316600
In this execution the value indicated 655316.6 microseconds.
It can also be done (of course) without Ada.Calendar.Formatting, like this for example:
with Ada.Calendar; use Ada.Calendar;
...
type Seconds_In_Day is range 0 .. 86_400;
-- Or use Integer if it is 32 bits.
Now : constant Day_Duration := Seconds (Clock);
Subsec : Duration := Now - Day_Duration (Seconds_In_Day (Now));
...
if Subsec < 0.0 then
-- Conversion of Now rounded up instead of down.
Subsec := Subsec + 1.0;
end if;
with the result in Subsec.
But using Ada.Calendar.Formatting.Sub_Second is shorter, and may be better (faster or more accurate) for all I know; I did not compare the two methods.
Many thaks for yours answers.
Using all yours examples, i made some trials, one is below :
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;
with Ada.Real_Time; use Ada.Real_Time;
procedure Display_Current_Year is
--need to precise the origin package Ada.Real-Time else ambiguous
Now : Ada.Calendar.Time := Clock;
Now_Year : Year_Number;
Now_Month : Month_Number;
Now_Day : Day_Number;
Now_Seconds : Day_Duration;
Current_Real_Time : Ada.Real_Time.Time;
Time_Span : Ada.Real_Time.Time_Span;
Seconds_Count : Ada.Real_Time.Seconds_Count;
Hour : float;
begin
--- Ada.Calendar
Split (Now,
Now_Year,
Now_Month,
Now_Day,
Now_Seconds);
Put_Line("Calendar : Date du jour = ");
Put_Line ("Current year is: "
& Year_Number'Image (Now_Year));
Put_Line ("Current month is: "
& Month_Number'Image (Now_Month));
Put_Line ("Current day is: "
& Day_Number'Image (Now_Day));
Put_Line ("'Current' seconde is: "
& Day_Duration'Image (Now_Seconds));
New_Line;
--Ada.Real_Time;
Current_Real_Time := Ada.Real_Time.Clock;
Ada.Real_Time.Split (T => Current_Real_Time,
Sc => Seconds_Count,
Ts => Time_Span);
Put_Line ("Real_Time : Seconds_Count = " & Seconds_Count'Img);
Hour := (float(Seconds_count) / 3600.00);
Put_Line ("Hour since seconds origin : "
& (Hour'Img));
end Display_Current_Year;
with result :
$ ./display_current_year
Calendar : Date du jour =
Current year is: 2022
Current month is: 2
Current day is: 27
'Current' seconde is: 68625.325897000
Real_Time : Seconds_Count = 30953
Hour since seconds origin : 8.59806E+00
$
-Results for calendar are OK, but why 30953 seconds !!
Where does GNAT take the Epoch, if this is, in this case, please?
Thanks
Mark
You can do a dirty trick where you define a record My_raw_duration_Type : whole_part, fraction_part, both U32. Define Unchecked_Conversion To_Raw (Ada.Real_Time.Duration, My_Raw_Duration_Type). Then take the result of that and call it My_Raw_Duration. The milliseconds result you want is integer(float(My_Raw_Duration.Fraction_Part)/float(4*1032**2) * 1000.0);

How do a do "while loop" in ocl when server side or Async?

Im using a rest api for getting orders from a webshop, the result is max 100 orders at a time and there is a page param to use for next 100 orders.
I have solve this in a viewmodel using periodic action (disables = not self.oclIsIn(#InProgress)) like this:
vCount := self.Orders->size;
vPage := vPage + 1;
vResponse := selfVM.RestGet(
String.Format('{0}/wp-json/wc/v3/orders?per_page={1}&page={2}',self.Channel.Url, self.Channel.Source.MaxCount, vPage),
self.Channel.UserName,
self.Channel.Password,'');
vResponse := String.Format('{0}{1}{2}','{"orders":', vResponse,'}');
--self.JSonData := self.JSonData + vResponse;
vOutput := vOutput + self.MergeTaJson( ImportBatch.Viewmodels.WooOrderJSon, vResponse );
if ((self.Orders->size - vCount) = 0) then
if vOutput->IsNullOrWhiteSpace then
self.Fail
else
--self.ParsingLog := vOutput;
self.Success
endif;
true
else
vCount := self.Orders->size;
false
endif
It will go on and on until result from the api is 0 then state sets to Done and it stops.
Now I want to do the same server side, but how do I solve this without a periodic action like this? And I want to do the same in a AsyncTicket "action".
Is it possible for you to see the whole serverside job as a periodic action?
Each execution of a SS job should fit in "one go" and saved in one transaction

TwinCAT 3 : Block stays busy

I want to save some data into a .txt file. I can create it, but I can't write text into it because the FB stays busy.
CASE counter OF
1:
fileOpen.sNetId := '';
fileOpen.sPathName := 'C:\test\test.txt';
fileOpen.nMode := FOPEN_MODEWRITE;
fileOpen.bExecute := TRUE;
fileOpen.tTimeout := T#200MS;
fileOpen();
counter := 2;
2:
IF NOT fileOpen.bBusy AND NOT fileOpen.bError THEN (* bBusy stays true*)
counter := 3;
END_IF
3:
fileOpen.bExecute := FALSE;
fbPutFile(sNetId := '', hFile := fileOpen.hFile, sLine := 'FOO', bExecute := TRUE, tTimeout := INT_TO_TIME(200), bBusy =>, bError =>, nErrId =>);
END_CASE
I get into step 2, but I can't get into step 3. Where is the problem?
It's because you have stopped calling the function block for opening the file. What you're doing here is that you're calling the FB_FileOpen in step 1, but then stop calling it. The bBusy-flag goes high (true) in step1, but can never be changed to anything else as you never call the FB again. You need to make a call to it in step2 as well. Remember that bExecute is also a trigger for the ADS-command, so once you've done it in step 2, you can set the bExecute-input to FALSE in step2 and forward as you've already triggered the command.
...
2:
fileOpen(bExecute := false);
IF NOT fileOpen.bBusy AND NOT fileOpen.bError THEN (* bBusy stays true*)
counter := 3;
END_IF
...
Also remember that Beckhoffs FB_FileOpen assumes that the directory C:\test exists (though not the file test.txt if you open it with FOPEN_MODEWRITE it will be automatically created). To create all necessary directores (if they don't already exist) use TwinCATs FB_CreateDir.

AHK: Sending Email Issue

I use below code to send email using AHK. It works when I use literal strings in subject and message body. But when I try to use %variable%, the resultant email received has blank subject/message body.
Order :=
Order = %Order% `n FAX MESSAGE
Order = %Order% `n
Order = %Order% `n Sent: %DateString% %TimeString%
pmsg := ComObjCreate("CDO.Message")
pmsg.From := """Lens Shapers"" <fax#l******s.com>"
pmsg.To := "k*******r#gmail.com"
pmsg.BCC := ""
pmsg.CC := ""
pmsg.Subject := "Lenses are Ready" **;THIS SUBJECT IS TRANSMITTED GOOD**
pmsg.TextBody := %Order% **;THIS MESSAGE BODY IS BLANK WHEN EMAIL IS RECEIVED**
sAttach := ""
fields := Object()
fields.smtpserver := "smtp.gmail.com" ; specify your SMTP server
fields.smtpserverport := 465 ; 25
fields.smtpusessl := True ; False
fields.sendusing := 2 ; cdoSendUsingPort
fields.smtpauthenticate := 1 ; cdoBasic
fields.sendusername := "k*****f#l******s.com"
fields.sendpassword := "PASSWORD"
fields.smtpconnectiontimeout := 60
schema := "http://schemas.microsoft.com/cdo/configuration/"
pfld := pmsg.Configuration.Fields
For field,value in fields
pfld.Item(schema . field) := value
pfld.Update()
Loop, Parse, sAttach, |, %A_Space%%A_Tab%
pmsg.AddAttachment(A_LoopField)
pmsg.Send()
It should work, but when you use a variable in an expression, you don't use the %'s and when you use ":=" that is an assignment of an expression. So try:
pmsg.TextBody := Order
As that should work. If you also need text elements, you need to use quotes around literals and the dot concatenation:
pmsg.TextBody := "Order: " . Order . "`n`n" ; also follows with 2 new lines
Hth,

Math not working as expected

I can't figure out how to use a variable as a math expression. The following code won't return the expected solution but doesnt throw any errors, instead it will just output the contents of the Q variable again in A.
Q = 7+5=
StringReplace, Q, Q, =,, 1
A := Q
Using %Q% or Q = %Q% doesn't work. And running MsgBox, %A% after it will just return "7+5".
Please help as I'm pretty new to AutoHotKey :)
Check this out:
http://www.autohotkey.com/community/viewtopic.php?t=17058
This should do it though:
Q = 7+5=
StringReplace, Q, Q, =,, 1
StringReplace, Q, Q, +,%A_SPACE%, 1
stringleft, first, Q, 1
stringright, last, Q, 1
x := first + last
MsgBox, %first%, %last%, %x%