Make .oct files for distmesh - octave

Distmesh is a popular triangulation routine written by Perrson in matlab. In some cases Persson uses .mex files to speed up the calculations. I want to extend this to octave. I have read https://enacit1.epfl.ch/octave_doc/octave/octave.html/Getting-Started-with-Oct_002dFiles.html and other tutorials and have succesfully made those examples work. Here is a typivcal code from distmesh:
function d=dpoly(p,pv)
% Copyright (C) 2004-2012 Per-Olof Persson. See COPYRIGHT.TXT for details.
np=size(p,1);
nvs=size(pv,1)-1;
ds=dsegment(p,pv);
%ds=zeros(np,nvs);
%for iv=1:nvs
% ds(:,iv)=donesegment(p,pv(iv:iv+1,:));
%end
d=min(ds,[],2);
d=(-1).^(inpolygon(p(:,1),p(:,2),pv(:,1),pv(:,2))).*d;
% MEXED
%function ds=donesegment(p,pv)
%
%e=ones(size(p,1),1);
%
%v=diff(pv,1);
%w=p-e*pv(1,:);
%
%c1=sum(w.*v(e,:),2);
%c2=sum(v(e,:).^2,2);
%
%ds=0*e;
%
%ix=c1<=0;
%ds(ix)=sqrt(sum((p(ix,:)-pv(1*ones(sum(ix),1),:)).^2,2));
%
%ix=c1>=c2;
%ds(ix)=sqrt(sum((p(ix,:)-pv(2*ones(sum(ix),1),:)).^2,2));
%
%ix=c1>0 & c2>c1;
%nix=sum(ix);
%if nix>0
% Pb=ones(nix,1)*pv(1,:)+c1(ix)./c2(ix)*v;
% ds(ix)=sqrt(sum((p(ix,:)-Pb).^2,2));
%end
The task in to convert function d=donesegment(p,v) (everything beneath the line % MEXED) into a .oct file. I am neither a c or cpp programmer, so I don't know what headers need to be includesd, or the statements needed to include them.

John Burkhardt's site has a copy of distmesh that already contains pure-matlab versions of the routines that are written in C++. Specifically dsegment.m is available there. I have run distmesh in octave using that file; it is a little slow but acceptable for small-to-medium size cases.

Related

How to access VHDL signal attributes in ModelSim via TCL?

I am developing a CPU in VHDL. I am using ModelSim for simulation and testing. In the simulation script I load a program from a binary file to the instruction memory. Now I want to automatically check if the program fits into memory and abort simulation if it doesn't. Since the memory is basically an array of std_logic_vectors, all I would have to do is read the corresponding signal attribute for use in a comparison. My problem is: How do I access a VHDL signal attribute in TCL inside ModelSim?
The closest I have gotten so far is to use the describe command:
describe sim/:tb:uut:imem:mem_array
which prints something like
# Array(0 to 255) [length 256] of
# Array(31 downto 0) [length 32] of
# VHDL standard subtype STD_LOGIC
Now, of course I could parse the length out of there via string operations. But that would not be a very generic solution. Ideally I would like to have something like this:
set mem_size [get_attribute sim/:tb:uut:imem:mem_array'length]
I have searched stackoverflow, googled up and down and searched through the commands in the command reference manual, but I could not find a solution. I am confident there must be a rather easy solution and I just lack the proper wording to successfully search for it. To me, this doesn't look overly specific and I am sure this could come in hand on many occasions when automating design testing. I am using version 10.6.
I would be very grateful if an experienced ModelSim user could help me out.
Disclaimer: I'm not a Tcl expert, so there's probably a more optimized solution out there.
There's a command called examine that you can use to get the value of obejcts.
I created a similar testbench here with a 256 x 32 array, the results were
VSIM> examine -radix hex sim/:tb:uut:imem:mem_array
# {32'hXXXXXXXX} {32'hXXXXXXXX} {32'hXXXXXXXX} {32'hXXXXXXXX} {32'hXXXXXXXX} ...
This is the value of sim/:tb:uut:imem:mem_array at the last simulation step (i.e.,
now).
The command return a list of values for each match (you can use wildcards), so
in our case, it's a list with a single item. You can get the depth by counting
the number of elements it returns:
VSIM> llength [lindex [examine sim/:tb:uut:imem:mem_array] 0]
# 256
You can get the bit width of the first element by using examine -showbase -radix hex,
which will return 32'hFFFFFFFF, where 32'h is the part you want to parse. Wrapping
that into a function would look like
proc get_bit_width { signal } {
set first_element [lindex [lindex [examine -radix hex -showbase $signal] 0] 0]
# Replace everything after 'h, including 'h itself to return only the base
return [regsub "'h.*" $first_element ""]
}
Hope this gives some pointers!
So, I actually found an easy solution. While further studying of the command reference manual brought to light that it is only possible to access a few special signal attributes and length is not one of them, I noticed that ModelSim automatically adds a size object to its object database for the memory array. So I can easily use
set ms [examine sim/:tb:uut:imem:mem_array_size]
to obtain the size and then check if the program fits.
This is just perfect for me, elegant and easy.

Publishing a report including a relative path to an image file via `publish`

I've read, tested this documentation and this example and tried on my own, but have not yet found a solution for the described issue.
According to the documentation, either the image file is on the same path of the used MATLAB script, in which case one could easily add it as described
% <<Filename.png>>
or one needs to add the full path:
% <<C:\\Fullpath\Filename.png>>
otherwise one would get an empty document (at least when publish is creating .html files).
As the documented code should be shared I need a relative path to avoid later issues.
Is there any solution or workaround within MATLAB?
Some of my former attempts:
% <<.\Filename.png>> # with and without "" or ''
% <<..\Filename.png>> # with and without "" or ''
% <<..\..\Filename.png>> # with and without "" or ''
The image does not appear.
Assuming this directory structure:
This code,
function q51539865
%% Image From URL
%
% <<https://i.stack.imgur.com/zSPip.jpg>>
%
%% Image From Relative Path
%
% <<../InnerPath/zSPip.jpg>>
%
Produces the following web page:
When inspecting the 2nd image we see:

How to add Latex-Style Terms in html tables in matlab publish reports

I've lately started to create documentation for some Matlab/Simulink and Stateflow codes/models etc after around ten years Matlab abstinence.
When documenting a Matlab (R2017b) code I can use the built-in function publish to create a documentation which uses both html and latex format. However for Simulink models it seems I'll need to limit myself on html documentation as long as I don't want to use the Report-Toolbox (for which I have no licence).
Now I want to create tables (with html Markup) where I'd like to insert latex equations or terms, but it seems I'll need a html to latex converter for that (See screenshot : Equation is created in latex format, the table in html, then there should appear a table in latex-format including the same terms below the text).
When I try it -in publish- by using latex tables as described in the latex Matlab help for LaTEX Markup:
%% LaTeX Markup Example
% <latex>
% \begin{tabular}{|r|r|}
% \hline $n$&$n!$\\
% \hline 1&1\\ 2&2\\ 3&6\\
% \hline
% \end{tabular}
% </latex>
%
the given table actually doesn't appear (or doesn't work) in the created html-file. I suppose it would appear if I choose to print the file in tex-format (I couldn't so far create a pdf-file from the created tex-file ->ERROR), but that doesn't seem satisfactory. I must admit my latex skills are very basic and due to the fact that my documentation wouldn't be restricted to m-files I would rather look for a html-option.
I'd like to know if there's a way to solve this issue with or (preferably) without external software. If possible with a short instruction.

Setting Octave interpreter to 'tex'

Such a simple question to have wasted an hour or two of my time. The Octave docs allude to setting the interpreter to tex and never say how to do it. I've looked on line and through stackoverflow and haven't found how to do this. I've also looked at the .octaverc files and have seen nothing that would indicate how to turn on the tex edit function. I am using Debian GNUOctave version 4.0.0. Please help.
Gary Roach
The interpreter property is set to "tex" per default for axes, line, text, patch and surface. So changing interpreter makes only sense if you want to switch to "none":
set (findobj (gcf, "-property", "interpreter"), "interpreter", "none")
This sets "interpreter"="none" for al children of the current figure.
If you want to have some fancy latex stuff in your plots and not only simple tex commands you can render it with latex:
close all
graphics_toolkit fltk
sombrero ();
title ("The sombrero function:")
fcn = "$z = \\frac{\\sin\\left(\\sqrt{x^2 + y^2}\\right)}{\\sqrt{x^2 + y^2}}$";
text (0.5, -10, 1.8, fcn, "fontsize", 20);
print -depslatexstandalone sombrero
## process generated files with pdflatex
system ("latex sombrero.tex");
## dvi to ps
system ("dvips sombrero.dvi");
## convert to png
system ("gs -dNOPAUSE -dBATCH -dSAFER -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r100x100 -dEPSCrop -sOutputFile=sombrero.png sombrero.ps")
which gives:

How to open .m files in octave on OSX 10.6.8

This file ex1.m, I "open with" octave, but it doesn't do anything. the first few lines of code are....
%% Machine Learning Online Class - Exercise 1: Linear Regression
% Instructions
% ------------
%
% This file contains code that helps you get started on the
% linear exercise. You will need to complete the following functions
% in this exericse:
%
% warmUpExercise.m
% plotData.m
% gradientDescent.m
% computeCost.m
% gradientDescentMulti.m
% computeCostMulti.m
% featureNormalize.m
% normalEqn.m
%
% For this exercise, you will not need to change any code in this file,
% or any other files other than those mentioned above.
%
% x refers to the population size in 10,000s
% y refers to the profit in $10,000s
%
%% Initialization
clear ; close all; clc
%% ==================== Part 1: Basic Function ====================
% Complete warmUpExercise.m
fprintf('Running warmUpExercise ... \n');
fprintf('5x5 Identity Matrix: \n');
warmUpExercise()
fprintf('Program paused. Press enter to continue.\n');
pause;
etc.... There were more files, I'm not sure how to get this to run...
This looks like part of Andrew Ng's Online Machine Learning class offered from www.coursera.org You might have more luck asking these questions in the forums at the class website after attempting to follow the tutorials yourself.
You need to have all of the relevant files in the directory first. Then actually complete the assignment. Most of those assignments do nothing until you complete the code section.
This file contains code that helps you get started on the linear exercise. You will need to complete the following functions in this exericse:
warmUpExercise.m
plotData.m
gradientDescent.m
computeCost.m
gradientDescentMulti.m
computeCostMulti.m
featureNormalize.m
normalEqn.m
After you are done, open a mac Terminal and cd to the directory where ex1.m is located. You can run the program by starting octave and entering the name of the file without the .m extension. Type ex1. Alternatively, you could type octave -q ex1.m from the terminal if you don't want to be in the octave shell after the program is done running.