Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a "Rosetta Stone" to the README #100

Closed
sebasguts opened this issue Jan 26, 2019 · 11 comments
Closed

Add a "Rosetta Stone" to the README #100

sebasguts opened this issue Jan 26, 2019 · 11 comments
Assignees

Comments

@sebasguts
Copy link
Contributor

It would be nice to have a small list of comparisons between polymake and Polymake.jl code in the README, i.e., lines like

$x->VERTICES => x.VERTICES
@kalmarek
Copy link
Contributor

First draft:

Rosetta stone:

Variables

Polymake Julia
$p (reference to 'scalar' variable) p (reference to any variable)
print $p; print(p) or println(p) or @show p, or just p in REPL
$i=5; $j=6; i,j = 5,6 or i=5; j=6 (; is needed for separation,
can be used to suppress return value in REPL)
$s = $i + $j; print $s; s = i + j

Arrays

Polymake Julia
Linear containers with random access Linear containers with random access + all the algebra attached
@A = ("a", "b", "c"); A = ["a", "b", "c"]
$first = $A[0];
(first is equal to a)
first = A[1]
(note the 1-based indexing!)
@A2 = (3,1,4,2); A2 = [3,1,4,2]
print sort(@A2);
(a copy of A2 is sorted)
println(sort(A2))
(to sort in place use sort!(A2))
$arr = new Array<Int>([3,2,5]);
(a C++ object)
arr = Int[3,2,5]
(although the type would have been inferred)
$arr->[0] = 100;
(assignment)
arr[1] = 100
(assignment; returns 100)

Dictionaries/Hash Tables

Polymake Julia
%h = (); h = Dict()
it is MUCH better to provide types, e.g.
h = Dict{String, Int}()
$h{"zero"}=0; $h{"four"}=4; h["zero"] = 0; h["four"] = 4
(call returns the value)
print keys %h; @show keys(h) (order is not specified)
print join(", ",keys %hash); join(keys(h), ", ")
(returns String)
%hash=("one",1,"two",2); Dict([("one",1), ("two",2)])
(will infer types)
%hash=("one"=>1,"two"=>2); Dict("one"=>1,"two"=>2)
(will infer types)

Sets

Polymake Julia
Balanced binary search trees Hash table with no content
$set=new Set<Int>(3,2,5,3); set = Set{Int}([3,2,5,3])
print $set->size; length(set)
@array_from_set=@$set collect(set)
(creates a Vector , order is not specified)

Matrices

Polymake Julia
Containers with algebraic operations Matrix{T} = Array{T, 2} – (linear) container of elements of type T with available indexing by 2-ples; all algebra attached
$mat=new Matrix<Rational>([[2,1,4,0,0],[3,1,5,2,1],[1,0,4,0,6]]); mat = Rational{Int}[2 1 4 0 0; 3 1 5 2 1; 1 0 4 0 6]
$row1=new Vector<Rational>([2,1,4,0,0]); $row2=new Vector<Rational>([3,1,5,2,1]); $row3=new Vector<Rational>([1,0,4,0,6]);
@matrix_rows=($row1,$row2,$row3); (Perl object)
$matrix_from_array=new Matrix<Rational>(\@matrix_rows);(C++ object)
row1 = Rational{Int}[2, 1, 4, 0, 0]; row2 = Rational{Int}[3, 1, 5, 2, 1]; row3 = Rational{Int}[1, 0, 4, 0, 6];
matrix_rows = hcat(row1', row2', row3')
(Julia stores matrices in column major format, so ' i.e. transposition is needed)
$mat->row(1)->[1]=7; $mat->elem(1,2)=8; mat[2,2] = 7; mat[2,3] = 8
$unit_mat=4*unit_matrix<Rational>(3); unit_mat = Diagonal([4//1 for i in 1:3])
or UniformScalin(4//1), depending on application (both require using LinearAlgebra)
$dense=new Matrix<Rational>($unit_mat); Array(unit_mat)
$m_rat=new Matrix<Rational>(3/5*unit_matrix<Rational>(5));
$m2=$mat/$m_rat;
m_rat = Diagonal([3//5 for i in 1:5])
m2 = mat/m_rat
$m_int=new Matrix<Int>(unit_matrix<Rational>(5));
$m3=$m_rat/$m_int;
(results in an error due to incompatible types)
m_int = Diagonal([1 for i in 1:5])
m_rat/m_int
(succeeds due to promote happening in /)
convert_to<Rational>($m_int) convert(Diagonal{Rational{Int}}, m_int)
$z_vec=zero_vector<Int>($m_int->rows)
$extended_matrix=($z_vec|$m_int);
(adds z_vec as the first column, result is dense)
z_vec = zeros(Int, size(m_int, 1))
extended_matrix = hcat(z_vec, m_int)
(result is sparse)
$set=new Set<Int>(3,2,5);
$template_Ex=new Array<Set<Int>>((new Set<Int>(5,2,6)),$set)
set = Set([3,2,5]); template_Ex = [Set([5,2,6]), set]
$p=new Polytope<Rational>(POINTS=>cube(4)->VERTICES);
$lp=new LinearProgram<Rational>(LINEAR_OBJECTIVE=>[0,1,1,1,1]);
$p->LP=$lp;
p->LP->MAXIMAL_VALUE;
p = p = perlobj("Polytope", :POINTS=>Polymake.polytope.cube(4).VERTICES)
lp = perlobj("LinearProgram", :LINEAR_OBJECTIVE=>[0,1,1,1,1])
p.LP = lp
p.LP.MAXIMAL_VALUE
$i = ($p->N_FACETS * $p->N_FACETS) * 15; i = (p.N_FACETS * p.N_FACETS) * 15

@kalmarek
Copy link
Contributor

It'd be preferable to call p = Polytope(:POINTS=>[1 0 1; 1 0 -1; 1 1 0; 1 -1 0]) etc. instead of perlobj("Polytope", ...), which requires resolution to the BigObjects-sometimes-need-template-parameter problem

@saschatimme
Copy link
Collaborator

saschatimme commented Feb 22, 2019

Maybe we could also just make a macro?

E.g.

p = @pm Polytope(:POINTS=>[1 0 1; 1 0 -1; 1 1 0; 1 -1 0])

@sebasguts
Copy link
Contributor Author

That way we could do Polytope<Rational>, right?

@sebasguts
Copy link
Contributor Author

I like that solution pretty much :). May I implement it? I have not yet worked with the Julia macros, and would like to learn it a bit more :)

@kalmarek
Copy link
Contributor

@pm Polytopes.Polytope{Rational}(...)

@saschatimme
Copy link
Collaborator

Macros still need to be valid Julia syntax. So Polytope{Rational} you probably need to have.

@kalmarek
Copy link
Contributor

kalmarek commented Feb 22, 2019

what redundancy checks:-)

@sebasguts
Copy link
Contributor Author

sebasguts commented Feb 22, 2019

@saschatimme The () seem fine to me, too. We do not need to jackhammer all polymake syntax into Julia.

On the other hand, Cxx.jl does some pretty fancy and shady stuff with macros. Are you completely sure you really need to have valid Julia syntax?

@sebasguts
Copy link
Contributor Author

But while this is discussion got out of scope pretty fast
@kalmarek I like this draft already. Do you like to make a PR to the readme? We can always adjust it to the syntax changes we make.

@saschatimme
Copy link
Collaborator

I think the PR is great now, and if we add such a macro we just update this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants