<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import numpy as np <EOL> def mesh ( N , edges = True ) : <EOL> if not edges : <EOL> N = N - <NUM_LIT:1> <EOL> tups = [ ] <EOL> for count , x1 in enumerate ( np . linspace ( <NUM_LIT:0.> , <NUM_LIT:1.> , num = N , endpoint = edges ) ) : <EOL> for x2 in np . linspace ( <NUM_LIT:0.> , <NUM_LIT:1.> - x1 , num = N - count , endpoint = edges ) : <EOL> x3 = <NUM_LIT:1> - x1 - x2 <EOL> tups . append ( ( x1 , x2 , x3 ) ) <EOL> tups = np . array ( tups ) <EOL> if not edges : <EOL> tups = tups [ np . logical_not ( ( tups == <NUM_LIT:0> ) . any ( axis = <NUM_LIT:1> ) ) ] <EOL> return tups <EOL> def meshnd ( D , N ) : <EOL> tups = [ ] <EOL> def _compute_points_helper ( x , C , stick , d ) : <EOL> if d == D : <EOL> tups . append ( x ) <EOL> else : <EOL> for c , xd in enumerate ( np . linspace ( <NUM_LIT:0.> , stick , num = C ) ) : <EOL> _compute_points_helper ( x + ( xd , ) , C - c , stick - xd , d + <NUM_LIT:1> ) <EOL> _compute_points_helper ( ( ) , N , <NUM_LIT:1.0> , <NUM_LIT:1> ) <EOL> tups = np . array ( tups ) <EOL> tups = np . hstack ( ( tups , ( <NUM_LIT:1.0> - tups . sum ( axis = <NUM_LIT:1> ) ) [ : , None ] ) ) <EOL> return tups <EOL> def nonlinear_meshnd ( D , N = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> inds = np . array ( np . unravel_index ( np . arange ( N ** D ) , ( N , ) * D ) ) <EOL> inds = inds . T <EOL> inds = inds . astype ( np . float ) <EOL> inds = inds [ <NUM_LIT:1> : ] <EOL> pts = inds / inds . sum ( <NUM_LIT:1> ) [ : , None ] <EOL> pts = np . unique ( pts ) <EOL> assert np . allclose ( pts . sum ( <NUM_LIT:1> ) , <NUM_LIT:1.0> ) <EOL> assert np . amin ( pts ) >= <NUM_LIT:0> <EOL> assert np . amax ( pts ) <= <NUM_LIT:1> <EOL> return pts <EOL> def proj_to_2D ( points ) : <EOL> return np . dot ( points , np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , - <NUM_LIT:0.5> ] , [ - <NUM_LIT:1> , - <NUM_LIT:0.5> ] ] ) ) <EOL> def _get_projector ( n ) : <EOL> foo = np . ones ( ( n , n - <NUM_LIT:1> ) ) <EOL> foo [ np . arange ( n - <NUM_LIT:1> ) , np . arange ( n - <NUM_LIT:1> ) ] = - ( n - <NUM_LIT:1> ) <EOL> Q , R = np . linalg . qr ( foo ) <EOL> return Q <EOL> def proj_vec ( v ) : <EOL> v = np . array ( v , ndmin = <NUM_LIT:2> ) <EOL> Q = _get_projector ( v . shape [ <NUM_LIT:1> ] ) <EOL> return np . dot ( v , Q ) <EOL> def proj_matrix ( mat ) : <EOL> Q = _get_projector ( mat . shape [ <NUM_LIT:1> ] ) <EOL> return Q . T . dot ( mat ) . dot ( Q ) <EOL> def test ( ) : <EOL> from matplotlib import pyplot as plt <EOL> mesh3D = mesh ( <NUM_LIT> , edges = True ) <EOL> mesh2D = proj_to_2D ( mesh3D ) <EOL> plt . figure ( ) ; plt . plot ( mesh2D [ : , <NUM_LIT:0> ] , mesh2D [ : , <NUM_LIT:1> ] , '<STR_LIT>' , label = '<STR_LIT>' ) <EOL> mesh3D_noedges = mesh ( <NUM_LIT> , edges = False ) <EOL> mesh2D_noedges = proj_to_2D ( mesh3D_noedges ) <EOL> plt . plot ( mesh2D_noedges [ : , <NUM_LIT:0> ] , mesh2D_noedges [ : , <NUM_LIT:1> ] , '<STR_LIT>' , label = '<STR_LIT>' ) <EOL> plt . legend ( ) <EOL> from mpl_toolkits . mplot3d import Axes3D <EOL> fig = plt . figure ( ) <EOL> ax = fig . add_subplot ( <NUM_LIT> , projection = '<STR_LIT>' ) <EOL> ax . scatter ( mesh3D [ : , <NUM_LIT:0> ] , mesh3D [ : , <NUM_LIT:1> ] , mesh3D [ : , <NUM_LIT:2> ] , c = '<STR_LIT:b>' ) <EOL> plt . show ( ) </s>
