Ask Your Question

zhaiyu's profile - activity

2023-10-10 14:41:36 +0200 received badge  Notable Question (source)
2023-10-10 14:41:26 +0200 received badge  Notable Question (source)
2023-10-10 14:41:26 +0200 received badge  Popular Question (source)
2023-10-08 00:01:13 +0200 received badge  Notable Question (source)
2023-10-08 00:01:13 +0200 received badge  Popular Question (source)
2022-03-07 14:20:57 +0200 received badge  Popular Question (source)
2021-03-16 23:23:16 +0200 received badge  Nice Question (source)
2021-03-16 18:20:01 +0200 asked a question Oriented vertices of Polyhedron

Oriented vertices of Polyhedron Hi, I wonder what is the most convenient way to extract the ordered/oriented vertices of

2021-03-03 09:28:33 +0200 received badge  Supporter (source)
2021-02-25 10:46:48 +0200 received badge  Self-Learner (source)
2021-02-25 10:46:48 +0200 received badge  Teacher (source)
2021-02-24 18:07:13 +0200 answered a question Python kernel seriously slower than SageMath kernel

Noted from John's comments, I can confirm the difference stems from the coercion and conversion stuff.

Given script.py and script.sage (same contents):

from sage.all import *
import time

def run(num):
    cube = (polytopes.cube() * 37 / 45).change_ring(QQ)

    for i in range(num):
        hspace = Polyhedron(ieqs=[[(i+1)/(i+2), 1/(i+1), (i+2), (i+53)]]).change_ring(QQ)
        intersection = hspace.intersection(cube)

tik = time.time()
run(100)
print('time elapsed: {}'.format(time.time() - tik), 's')

With python script.py, I got time elapsed: 8.750308990478516 s.

With sage script.sage, I got time elapsed: 0.6618611812591553 s, whose execution automatically generated a python file script.sage.py which looks like this:

# This file was *autogenerated* from the file profile.sage
from sage.all_cmdline import *   # import sage library

_sage_const_37 = Integer(37); _sage_const_45 = Integer(45); _sage_const_1 = Integer(1); _sage_const_2 = $
from sage.all import *
import time

def run(num):
    cube = (polytopes.cube() * _sage_const_37  / _sage_const_45 ).change_ring(QQ)

    for i in range(num):
        hspace = Polyhedron(ieqs=[[(i+_sage_const_1 )/(i+_sage_const_2 ), _sage_const_1 /(i+_sage_const_$
        intersection = hspace.intersection(cube)

tik = time.time()
run(_sage_const_100 )
print('time elapsed: {}'.format(time.time() - tik), 's')

I then ran python script.sage.py and got time elapsed: 0.6441857814788818 s.

This conversion is documented in the tutorial:

When Sage loads example.sage it converts it to Python, which is then executed by the Python interpreter. This conversion is minimal; it mainly involves wrapping integer literals in Integer() floating point literals in RealNumber(), replacing ^’s by **’s, and replacing e.g., R.2 by R.gen(2). The converted version of example.sage is contained in the same directory as example.sage and is called example.sage.py.

2021-02-24 10:40:38 +0200 commented question Python kernel seriously slower than SageMath kernel

I think John made a good point. It's not fair to compare Python Jupyter notebook to [SageMath Jupyter notebook or SageNB]. For performance issue I would stick with a 'native' Sage runtime. But does this mean faster execution or better integration (i.e. import Sage as a library in a Python script) we can only pick one?

2021-02-24 10:25:51 +0200 commented question Python kernel seriously slower than SageMath kernel

Sorry for the confusion: I actually mean the comparison between the SageMath 9.1 kernel and the Python 3 kernel, both of which running in Jupyter. Question edited.

2021-02-23 23:42:31 +0200 commented question Python kernel seriously slower than SageMath kernel

My env:

macOS 10.15.6 (RAM 16GB) SageMath 9.1

2021-02-23 23:40:29 +0200 asked a question Python kernel seriously slower than SageMath kernel

Hi, I just found the Python kernel is seriously slower than the SageMath one.

This was the snippet to reproduce:

# intersection profiling with Python session
from sage.all import *

def run(num):
    cube = (polytopes.cube() * 37 / 45).change_ring(QQ) 

    for i in range(num):
        hspace = Polyhedron(ieqs=[[1/2, 1/(i+1), 0, 0]]).change_ring(QQ)
        intersection = hspace.intersection(cube)

%timeit run(100)

The timing for SageMath kernel is 1 loop, best of 5: 186 ms per loop, while that for Python is 1 loop, best of 5: 7.07 s per loop. With this snippet I think no variable was cached. I also cprofiled a stand-alone .py file with the same lines (expect the %timeit line), whose timing is close to the Python kernel. Any idea what could cause this huge performance difference?

Image which kernel to use

2021-02-22 15:16:57 +0200 received badge  Student (source)
2021-02-21 23:12:23 +0200 commented answer Union of touching polyhedra

That I know. I just found out the union thing can be done with https://doc.cgal.org/latest/Nef_3/index.html (3D Boolean Operations on Nef Polyhedra). While Sage doesn't have it (seems the Nef representation does exist for lattice polytope though), CGAL has it implemented. Thanks again.

2021-02-20 11:43:02 +0200 received badge  Editor (source)
2021-02-20 11:41:19 +0200 commented answer Union of touching polyhedra

Thanks for the answer! The union in the example is intentionally not convex. What I'd like to have in the end is the outer surface of the union (doesn't have to be Sage's polyhedron which as I understand should be convex; it can be polygons3d or just the boundary representation of the mesh). I checked your slabbe package (PolyhedronPartition seems close to my needs) but couldn't find the desired function.

2021-02-19 14:49:29 +0200 asked a question Union of touching polyhedra

Hi, I'm trying to do a union operation over multiple touching polyhedra into one but couldn't find how to achieve this with Sage. For example, how can I merge these two cubes and dissolve the interior face (the touching face):

# two cubes touching
cube_a = Polyhedron(vertices=[(0,0,0), (0,0,1), (0,1,0), (0,1,1), (1,0,0), (1,0,1), (1,1,0), (1,1,1)])
cube_b = Polyhedron(vertices=[(1,1/2,0), (1,1/2,1), (1,3/2,0), (1,3/2,1), (2,1/2,0), (2,1/2,1), (2,3/2,0), (2,3/2,1)])

# visualize
cube_a.plot() + cube_b.plot()

The polyhedra can be assumed all convex, and each touching pair share a polygon interface. I found polyhedron.intersection(other), but no polyhedron.union(other) or so. Any workaround I can do? Thanks in advance.