On 1/9/2026 11:37 PM, Jeong Dal via ntg-context wrote:
Dear Hans,

Thank you for the correction.

see below

your "decimal j" makes string while a number is expected

here mp.print is clever enough to return a number

   label(textext("A"),(
       i*u,
       lua("mp.print(MP.data[" & decimal i & "][1])")
   ));
   message(lua("mp.print(MP.data[" & decimal i & "][1])"));

Now I got the working code.
It calculates the list of 4-dimensional vectors and draw a line graph which shows the change of each component of vectors.

Here is the code. There may be several points which can be written more nicely.

Thanks again.

Best regards,

Dalyoung

\startluacode
local matT = {{0,0,1,1/3},{1,0,0,1/3},{0,1/2,0,1/3},{0,1/2,0,0}}
local matOld = {{1/4},{1/4},{1/4},{1/4}}
local matNew = {}
local matHistory = {}
table.insert(matHistory, matOld)
for i = 1, 15 do
matOld = matHistory[i]
matNew = mtx.product(matT, matOld)
if (i < 5) or (i==14) then
context("${\\mathbf P_{"..tostring(i).."} = }$") mtx.typeset(matT)context("\\times")mtx.typeset(matOld)
context(" = ") mtx.typeset(matNew) context("\\blank")
end
table.insert(matHistory, matNew)
end
table.save("tempList.lua", matHistory)
\stopluacode

\startplacefigure[reference=fig:PR,title={PageRank 값의 변화}]
{\startMPcode
numeric u, yy; u := 1cm;
pair A[];
path p;
string prob;
color myColor[];
myColor[1] := .7red;
myColor[2] := .7blue;
myColor[3] := .7green;
myColor[4] := .7white;

lua("MP = { } MP.data = table.load('tempList.lua')") ;
for j = 1 upto 4:
for i = 1 upto 15:
yy := lua("mp.print(MP.data[" & decimal i & "][" & decimal j & "])");
A[i] := (i*.7u,yy*15u);
% message(lua("mp.print(MP.data[" & decimal i & "][1])"));
endfor;
p := for i = 1 upto 14 : A[i] -- endfor A[15] ;
prob := substring (0,6) of (decimal yy);
label.rt(textext(char(64+j) & " " & prob),A[15]);
draw p withpen pencircle scaled 1.5pt withcolor myColor[j];
drawpoints p;%[i];
endfor;
\stopMPcode}
\stopplacefigure

You have to be more careful here; you do

  MP = { }

which overloaded an official global namespace. That will backfire at some point.

You still have mtx as global alias without testing if it exists. Don't do that, because one never knows if context itself will use that namespace (ctx, mtx ...). Basically we don't expect users or modules to define global tables at all, and if they to one has it coming. Just use userdata or moduledata (with subtable) or so.

I'm not going to rewrite your code but here are a few suggestions:

    function MP.loadmydata(n)
        MP.mydata = table.load(n)
    end

    function MP.getmydata(i,j)
        mp.print(MP.mydata[i][j])
    end

and then

% lua("MP = { } MP.data = table.load('tempList.lua')") ;
lua.MP.loadmydata('tempList.lua') ;

with

%     yy := lua("mp.print(MP.data[" & decimal i & "][" &  decimal j & "])");
    yy := lua.MP.getmydata(i,j);

which gives you an easier way to check for instance of the data is ok or mess with it at the lua end.

Watch how we accept ' as string delimiter ... by default luametafun is set up to accept ' and " as in lua.

Hans

-----------------------------------------------------------------
                                          Hans Hagen | PRAGMA ADE
              Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
       tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : [email protected] / 
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

Reply via email to