1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
  
     | 
    
      newPackage(
    "Hadamard",
    Version => "0.1",
    Date => "November 2020",
    Authors => {
	{Name => "Iman Bahmani Jafarloo",
	    Email => "ibahmani89@gmail.com",
	    HomePage => "http://calvino.polito.it/~imanbj"
	    }
	}, -- TODO
    Headline => "Hadamard products of projective subvarieties",
    AuxiliaryFiles => false,
    DebuggingMode => false,
    Reload => false,
    PackageExports => {"Points"},
    Keywords => {"Commutative Algebra"}
    )
export {
    -- types
    "Point",
    -- methods
    "point",
    "hadamardProduct",
    "hadamardPower",
    "idealOfProjectivePoints"
    }
--defining a new type of objects: points
Point = new Type of BasicList
point=method()
point(VisibleList):=(P)->(
    if all(P, i->i==0) then error("all entries are zero") else
    if any(P,i->i===null) then error("null entries are not allowed")
    else new Point from P)
Point * Point:=(p,q)->(
    if #p =!= #q then error("points should be in a same projective space") else
    pp:=apply(p,q,times);
    if all(pp, i->i==0) then error("product of points has no nonzero coordinate")
    else return pp
    )
Point == Point := (p,q)->(
    rank pointsToMatrix({p,q}) == 1
    )
---Hadamard product of two points---
hadProdPoints = method()	 
hadProdPoints(Point,Point):=(p,q)->(
     p * q
    )
hadProdPoints(List,List):=(p,q)->(
 point p * point q
    )
hadProdPoints(Array,Array):=(p,q)->(
   point p * point q
    )
-- Hadamrd product of two varieties
hadProdOfVariety = method()
hadProdOfVariety (Ideal, Ideal):= (I,J) -> (
    newy := symbol newy;
    newz := symbol newz;
    varI:= gens ring I;
    varJ:= gens ring J;
    CRI:=coefficientRing ring I;
    CRJ:=coefficientRing ring J;
    RYI:=CRI[newy_0.. newy_(# varI -1)];
    RZJ:=CRI[newz_0..newz_(# varJ -1)];
    IY:=sub(I,apply(varI,gens RYI,(a,b)->a=>b));
    JZ:=sub(J,apply(varJ,gens RZJ,(a,b)->a=>b));
    TensorRingIJ:=RYI/IY ** RZJ/JZ;
    use TensorRingIJ;
    Projvars:=apply(#(gens ring I),i->newy_i * newz_i);
    hadMap:=map(TensorRingIJ,ring I, Projvars);
    ker hadMap
    )
-------Hadmard product of two subsets of points on two varieties---------
hadProdListsOfPoints = method()
hadProdListsOfPoints(List,List) :=(X,Y)->(
     convert:= obj -> if not instance(obj, Point) then point(obj) else obj;
     newX:=apply(X,convert);
     newY:=apply(Y,convert);
     return delete(null, for I in toList(set(newX) ** set(newY)) list (
	 try(I_0 * I_1) then (I_0 * I_1) else null
	 ))
     )
pointsToMatrix=method()
pointsToMatrix(List):= (PTM) ->( matrix apply(PTM, toList))
---Hadamard powers of varieties------------
hadamardPower = method()
hadamardPower(Ideal,ZZ):=(I,r)->(
    if r<1 then error("the second argument should be positive integer >=1");
   NewI := I;
   for i from 1 to r-1 do NewI = hadProdOfVariety(NewI,I);
   return NewI)
---Hadamard powers of sets of points ------------
hadamardPower(List,ZZ):=(L,r)->(
    if r<1 then error("the second argument should be a positive integer >=1");
   NewL := L;
   for i from 1 to r-1 do NewL = hadProdListsOfPoints(NewL,L);
   return toList set NewL)
-----------------%%%--------------------------------%%%---------------
hadamardMult=method()
hadamardMult(List):=(L)->(
    if not uniform L then
     error("entries should be in the same class");
    if instance(first L,Ideal) then fold(hadProdOfVariety,L)
    else
    if instance(first L,List) or instance(first L,Point) then fold(hadProdPoints,L)
    else error("input should be a list of ideals or points")
    )
-------general product------
hadamardProduct=method()
hadamardProduct(Ideal,Ideal):=(I,J)->(hadProdOfVariety(I,J))
hadamardProduct(List,List):=(X,Y)->(hadProdListsOfPoints(X,Y))
hadamardProduct(List):=(L)->(hadamardMult(L));
------------------Hadamard product ends ------------------
-----------------------new results-------------
idealOfProjectivePoints=method()
idealOfProjectivePoints(List,Ring):=(L,R)->(
    if not uniform L then 
     error("entries should be in the same class");
    MP:=transpose pointsToMatrix L; 
    return ideal projectivePointsByIntersection(MP,R)
    )
--------Terracini lemma------
-----------------------------------------------
beginDocumentation()
doc ///
     Key
       Hadamard
     Headline
       a package to study Hadamard products of varieties.
     Description
       Text
	 This package provides a class for representing points in projective
	 space and methods for computing Hadamard products of varieties.
///
doc ///
    Key
    	Point
    Headline
    	a new type for points in projective space
    Description
    	Text
	   A point in projective space is represented as an object in the class @TO Point@. An element of this class is a @TO BasicList@.
///
doc ///
    Key
       (symbol *, Point, Point)
    Headline
    	entrywise product of two projective points
    Usage
    	p * q
    Inputs
    	p:Point
	q:Point
    Outputs
    	:Point
    Description
    	Example
	    p = point {1,2,3};
	    q = point {-1,2,5};
	    p * q
	Text
	    Note that this operation is not always well-defined in projective space, 
	    e.g., the Hadamard product of the points $[1:0]$ and $[0:1]$ is not well-defined
///
doc ///
    Key
    	(symbol ==, Point, Point)
    Headline
    	check equality of two projective points
    Usage
    	p == q
    Inputs
    	p:Point
	q:Point
    Outputs
    	:Boolean
    Description
    	Example
	    p = point {1,1};
	    q = point {2,2};
	    p == q
///
doc ///
    Key
    	point
        (point, VisibleList)
    Headline
        constructs a projective point from the list (or array) of coordinates.
    Usage
        point(L)
    Inputs
        L:List 
	   or @TO2{Array, "array"}@
	   or @TO2{VisibleList, "visible list"}@
    Outputs
        :Point
    Description
        Example
            point {1,2,3}
            point [1,4,6]
///
doc ///
    Key
    	hadamardProduct
    Headline
        computes the Hadamard product of varieties
///
doc ///
    Key
	(hadamardProduct, Ideal, Ideal)
    Headline
         Hadamard product of two homogeneous ideals
    Usage
        hadamardProduct(I,J)
    Inputs
        I:Ideal
	    (homogeneous)
	J:Ideal
	    (homogeneous)
    Outputs
         :Ideal
    Description
        Text
            Given two projective subvarieties $X$ and $Y$, their Hadamard product is defined as the
	    Zariski closure of the set of (well-defined) entrywise products of pairs of points in the cartesian 
	    product $X \times Y$. This can also be regarded as the image of the Segre product of $X \times Y$
	    via the linear projection on the $z_{ii}$ coordinates. The latter is the way the function is implemented.
	
	    Consider for example the entrywise product of two points.
	Example
	    S = QQ[x,y,z,t];
	    p = point {1,1,1,2};
	    q = point {1,-1,-1,-1};
	    idealOfProjectivePoints({p*q},S)
	Text
	    This can be computed also from their defining ideals as explained.
	Example
	    IP = ideal(x-y,x-z,2*x-t)
	    IQ = ideal(x+y,x+z,x+t)
            hadamardProduct(IP,IQ)
	Text
	    We can also consider Hadamard product of higher dimensional varieties. 
	    For example, the Hadamard product of two lines.
	Example    
            I = ideal(random(1,S),random(1,S));
            J = ideal(random(1,S),random(1,S));
	    hadamardProduct(I,J)
///
doc ///
    Key
       (hadamardProduct, List)
    Headline
        Hadamard product of a list of homogeneous ideals, or points
    Usage
    	hadamardProduct(L)
    Inputs
    	L:List
            of @TO2{Ideal, "(homogeneous) ideals"}@ or @TO2{Point, "(projective) points"}@
    Outputs
    	:Ideal
	:Point
    Description
    	Text
	    The Hadamard product of a list of ideals or points constructed by using iteratively the binary function
	    @TO (hadamardProduct, Ideal, Ideal)@, or  @TO (symbol *, Point, Point)@.
	Example
	    S = QQ[x,y,z,t];
	    I = ideal(random(1,S),random(1,S));
            J = ideal(random(1,S),random(1,S));
	    L = {I,J};
	    hadamardProduct(L)
	    P = point\{{1,2,3},{-1,1,1},{1,1/2,-1/3}}
	    hadamardProduct(P)
///
doc ///
    Key
    	(hadamardProduct, List, List)
    Headline
        Hadamard product of two sets of points    
    Usage
    	hadamardProduct(L,M)
    Inputs
    	L:List
	    of @TO Point@
	M:List
	    of @TO Point@
    Outputs
    	:List
	    of @TO Point@
    Description
    	Text
	    Given two sets of points $L$ and $M$ returns the list of (well-defined) entrywise
	    multiplication of pairs of points in the cartesian product $L\times M$.
	Example
	    L = {point{0,1}, point{1,2}};
	    M = {point{1,0}, point{2,2}};
	    hadamardProduct(L,M)
///
doc ///
    Key
      hadamardPower
    Headline
        computes the Hadamard powers of varieties
///
doc ///
    Key
     (hadamardPower,Ideal, ZZ)
    Headline
        computes the $r$-th Hadmard powers of varieties
    Usage
        hadamardPower(I,r)
    Inputs
        I:Ideal
	   of @TO2{Ideal, "(homogeneous) ideals"}@ 
        r:ZZ
	  a positive integer $>=1$
    Outputs
         :Ideal
    Description
        Text
         Give a homogeneous ideal $I$, the $r$-th Hadamard power of $I$ is $r$-times Hadamard product of I to itself; $( I x\cdots x I)_{r-times}$
        Example
            S=QQ[x,y,z,w]
            I=ideal(random(1,S),random(1,S),random(1,S))
            hadamardPower(I,3)
///
doc ///
    Key
     (hadamardPower,List,ZZ)
    Headline
        computes the $r$-th Hadmard powers of a set points
    Usage
        hadamardPower(L,r)
    Inputs
        L:List 
	  of @TO2{Point, "(projective) points"}@
	r:ZZ
	  a positive integer $>=1$
    Outputs
         :List
    Description
        Text
         Give a set of points $L$, the $r$-th Hadamard power of $L$ is $r$-times Hadamard product of L to itself; $( L x\cdots x L)_{r-times}$
        Example
            L={point{1,1,1/2},point{1,0,1},point{1,2,4}}
            hadamardPower(L,3)
///
doc ///
    Key
     idealOfProjectivePoints
     (idealOfProjectivePoints,List,Ring)
    Headline
        computes the ideal of set of points
    Usage
        idealOfProjectivePoints(L,S)
    Inputs
        L:List
	   of @TO2{Point, "(projective) points"}@
	S:Ring
    Outputs
        I:Ideal
    Description
        Text
          Given a set points $X$, it returns the defining ideal of $I(X)$
        Example
            S = QQ[x,y,z] 
            X = {point{1,1,0},point{0,1,1},point{1,2,-1}}
            I = idealOfProjectivePoints(X,S)
            I2 = hadamardPower(I,2)
            X2 = hadamardPower(X,2)
            I2 == idealOfProjectivePoints(X2,S)
///
     TEST ///
     assert(point{1,2,3,4} * point{1,2,3,4}==point{1,4,9,16})
 
     -- may have as many TEST sections as needed
     ///
end
restart
uninstallPackage "Hadamard"
installPackage "Hadamard"
loadPackage ("Hadamard",Reload=>true)
viewHelp "Hadamard"
check "Hadamard"
 
     |