Révision 200

pobysoPythonSage/src/pobyso.py (revision 200)
273 273
    """
274 274
    Get a Sollya constant from a Sage int.
275 275
    """
276
    return(sollya_lib_constant_from_int(int(anInt)))
276
    return(sollya_lib_constant_from_int64(long(anInt)))
277 277

  
278 278
def pobyso_constant_from_int_so_sa(constSo):
279 279
    """
280 280
    Get a Sage int from a Sollya int constant.
281 281
    Usefull for precision or powers in polynomials.
282 282
    """
283
    constSa = c_int(0)
284
    sollya_lib_get_constant_as_int(byref(constSa), constSo)
283
    constSa = c_long(0)
284
    sollya_lib_get_constant_as_int64(byref(constSa), constSo)
285 285
    return(constSa.value)
286 286
# End pobyso_constant_from_int_so_sa
287 287

  
288
def pobyso_constant_from_mpq_sa_so(rationalSa, precision = None):
289
    """
290
    Make a Sollya constant from Sage rational.
291
    A bit convoluted to take into account precision and the fact
292
    that mpq constants in Sollya a non-evaluated expressions.
293
    Function building and evaluation is needed to make it a "real"
294
    evaluated constant.
295
    """
296
    ## Deal with precision stuff.
297
    curPrecSa = None
298
    curPrecSo = None
299
    if not precision is None:
300
        curPrecSo = pobyso_get_prec_so()
301
        curPrecSaSa = c_int(0)
302
        sollya_lib_get_constant_as_int(byref(curPrecSaSa), curPrecSo)
303
        curPrecSa = int(curPrecSaSa.value)
304
        if curPrecSa != precision:
305
            pobyso_set_prec_sa_so(precision)
306
    ## In Sollya mpq constants are non-evaluated expressions.
307
    #  We must force evaluation.
308
    zeroSo    = pobyso_constant_0_sa_so()
309
    oneSo     = pobyso_constant_1_sa_so()
310
    ratExprSo = \
311
        sollya_lib_constant_from_mpq(sgmp_get_rational_value(rationalSa))
312
    addExprSo = sollya_lib_build_function_add(zeroSo, ratExprSo)
313
    constSo   = sollya_lib_evaluate(addExprSo,oneSo)
314
    ## Clears expression and arguments, as the former was created with a
315
    #  "build" variant.
316
    sollya_lib_clear_obj(addExprSo)
317
    sollya_lib_clear_obj(oneSo)
318
    if curPrecSa != precision:
319
        pobyso_set_prec_so_so(curPrecSo)
320
        sollya_lib_clear_obj(curPrecSo)
321
    return constSo
322
# End pobyso_constant_from_mpq_sa_so.
323

  
288 324
def pobyso_error_so():
289 325
    return sollya_lib_error(None)
290 326
# End pobyso_error().
......
441 477
    return(rnSa)
442 478
# End pobyso_get_constant_as_rn_with_rf_so_sa
443 479

  
480
def pobyso_get_float_poly_sa_so(polySa, realFieldSa=None):
481
    """
482
    Create a Sollya polynomial from a Sage polynomial.
483
    """
484
    pass
485
# End get_float_poly_sa_so.
486
    
444 487
def pobyso_get_free_variable_name():
445 488
    """ 
446 489
    Legacy function. See pobyso_get_free_variable_name_so_sa.
......
588 631
    constExpAsRnSa = pobyso_get_constant_as_rn_so_sa(constExpSo)
589 632
    return(min_mpfr_size(get_rn_value(constExpAsRnSa)))
590 633

  
634
def pobyso_get_poly_so_sa(polySo, realFieldSa=None):
635
    """
636
    Convert a Sollya polynomial into a Sage polynomial.
637
    We assume that the polynomial is in canonical form.
638
    If no realField is given, a RealField corresponding to the maximum 
639
    precision of the coefficients is internally computed.
640
    The real field is not returned but can be easily retrieved from 
641
    the polynomial itself.
642
    ALGORITHM:
643
    - (optional) compute the RealField of the coefficients;
644
    - convert the Sollya expression into a Sage expression;
645
    - convert the Sage expression into a Sage polynomial
646
    TODO: the canonical thing for the polynomial.
647
    """    
648
    if realFieldSa is None:
649
        expressionPrecSa = pobyso_get_max_prec_of_exp_so_sa(polySo)
650
        realFieldSa = RealField(expressionPrecSa)
651
    #print "Sollya expression before...",
652
    #pobyso_autoprint(polySo)
653

  
654
    expressionSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, \
655
                                                             realFieldSa)
656
    #print "...Sollya expression after.",
657
    #pobyso_autoprint(polySo)
658
    polyVariableSa = expressionSa.variables()[0]
659
    polyRingSa = realFieldSa[str(polyVariableSa)]
660
    #print polyRingSa
661
    # Do not use the polynomial(expressionSa, ring=polyRingSa) form!
662
    polynomialSa = polyRingSa(expressionSa)
663
    return(polynomialSa)
664
# End pobyso_get_poly_so_sa
665

  
666
def pobyso_get_prec():
667
    """ Legacy function. See pobyso_get_prec_so_sa(). """
668
    return(pobyso_get_prec_so_sa())
669

  
670
def pobyso_get_prec_so():
671
    """
672
    Get the current default precision in Sollya.
673
    The return value is a Sollya object.
674
    Usefull when modifying the precision back and forth by avoiding
675
    extra conversions.
676
    """
677
    return(sollya_lib_get_prec(None))
678
    
679
def pobyso_get_prec_so_sa():
680
    """
681
    Get the current default precision in Sollya.
682
    The return value is Sage/Python int.
683
    """
684
    precSo = sollya_lib_get_prec(None)
685
    precSa = c_int(0)
686
    sollya_lib_get_constant_as_int(byref(precSa), precSo)
687
    sollya_lib_clear_obj(precSo)
688
    return int(precSa.value)
689
# End pobyso_get_prec_so_sa.
690

  
691
    
692
def pobyso_get_prec_of_constant(ctExpSo):
693
    """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
694
    return(pobyso_get_prec_of_constant_so_sa(ctExpSo))
695

  
696
def pobyso_get_prec_of_constant_so_sa(ctExpSo):
697
    """
698
    Tries to find a precision to represent ctExpSo without rounding.
699
    If not possible, returns None.
700
    """
701
    prec = c_int(0)
702
    retc = sollya_lib_get_prec_of_constant(byref(prec), ctExpSo, None)
703
    if retc == 0:
704
        return(None)
705
    return(int(prec.value))
706

  
707
def pobyso_get_prec_of_range_so_sa(rangeSo):
708
    """
709
    Returns the number of bits elements of a range are coded with.
710
    """
711
    prec = c_int(0)
712
    retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
713
    if retc == 0:
714
        return(None)
715
    return(int(prec.value))
716
# End pobyso_get_prec_of_range_so_sa()
717

  
718
def pobyso_get_rat_poly_sa_so(polySa, precSa = None):
719
    """
720
    Create a Sollya polynomial from a Sage rational polynomial.
721
    """
722
    ## TODO: filter arguments.
723
    ##
724
    if not precSa is None:
725
        initialPrecSo = pobyso_get_prec_so() 
726
    coefficients = polySa.coefficients()
727
    exponents    = polySa.exponents()
728
    ## TODO: deal with variables names.
729
    
730
    ## If necessary, return Sollya to its initial default precision.
731
    if not precSa is None:
732
        pobyso_set_prec_so_so(initialPrecSo)
733
# End pobyso_get_rat_poly_sa_so    
734
    
591 735
def pobyso_get_sage_exp_from_sollya_exp(sollyaExpSo, realField = RR):
592 736
    """ Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
593 737
    return(pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExpSo, \
......
640 784
# End pobyso_get_sage_exp_from_sollya_exp_so_sa
641 785

  
642 786

  
643
def pobyso_get_poly_sa_so(polySo, realFieldSa=None):
644
    """
645
    Create a Sollya polynomial from a Sage polynomial.
646
    """
647
    pass
648
# pobyso_get_poly_sa_so
649

  
650
def pobyso_get_poly_so_sa(polySo, realFieldSa=None):
651
    """
652
    Convert a Sollya polynomial into a Sage polynomial.
653
    We assume that the polynomial is in canonical form.
654
    If no realField is given, a RealField corresponding to the maximum 
655
    precision of the coefficients is internally computed.
656
    The real field is not returned but can be easily retrieved from 
657
    the polynomial itself.
658
    ALGORITHM:
659
    - (optional) compute the RealField of the coefficients;
660
    - convert the Sollya expression into a Sage expression;
661
    - convert the Sage expression into a Sage polynomial
662
    TODO: the canonical thing for the polynomial.
663
    """    
664
    if realFieldSa is None:
665
        expressionPrecSa = pobyso_get_max_prec_of_exp_so_sa(polySo)
666
        realFieldSa = RealField(expressionPrecSa)
667
    #print "Sollya expression before...",
668
    #pobyso_autoprint(polySo)
669

  
670
    expressionSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, \
671
                                                             realFieldSa)
672
    #print "...Sollya expression after.",
673
    #pobyso_autoprint(polySo)
674
    polyVariableSa = expressionSa.variables()[0]
675
    polyRingSa = realFieldSa[str(polyVariableSa)]
676
    #print polyRingSa
677
    # Do not use the polynomial(expressionSa, ring=polyRingSa) form!
678
    polynomialSa = polyRingSa(expressionSa)
679
    return(polynomialSa)
680
# End pobyso_get_sage_poly_from_sollya_poly
681

  
682 787
def pobyso_get_subfunctions(expressionSo):
683 788
    """ Legacy function. See pobyso_get_subfunctions_so_sa. """
684 789
    return(pobyso_get_subfunctions_so_sa(expressionSo)) 
685

  
790
# End pobyso_get_subfunctions.
791
 
686 792
def pobyso_get_subfunctions_so_sa(expressionSo):
687 793
    """
688 794
    Get the subfunctions of an expression.
......
724 830
        subs.append(int(subfunctions[i].value))
725 831
        #print subs[i]
726 832
    return(int(arity.value), subs)
833
# End pobyso_get_subfunctions_so_sa
727 834
    
728
def pobyso_get_prec():
729
    """ Legacy function. See pobyso_get_prec_so_sa(). """
730
    return(pobyso_get_prec_so_sa())
731

  
732
def pobyso_get_prec_so():
733
    """
734
    Get the current default precision in Sollya.
735
    The return value is a Sollya object.
736
    Usefull when modifying the precision back and forth by avoiding
737
    extra conversions.
738
    """
739
    return(sollya_lib_get_prec(None))
740
    
741
def pobyso_get_prec_so_sa():
742
    """
743
    Get the current default precision in Sollya.
744
    The return value is Sage/Python int.
745
    """
746
    precSo = sollya_lib_get_prec(None)
747
    precSa = c_int(0)
748
    sollya_lib_get_constant_as_int(byref(precSa), precSo)
749
    sollya_lib_clear_obj(precSo)
750
    return int(precSa.value)
751
# End pobyso_get_prec_so_sa.
752

  
753
def pobyso_get_prec_of_constant(ctExpSo):
754
    """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
755
    return(pobyso_get_prec_of_constant_so_sa(ctExpSo))
756

  
757
def pobyso_get_prec_of_constant_so_sa(ctExpSo):
758
    prec = c_int(0)
759
    retc = sollya_lib_get_prec_of_constant(byref(prec), ctExpSo, None)
760
    if retc == 0:
761
        return(None)
762
    return(int(prec.value))
763

  
764
def pobyso_get_prec_of_range_so_sa(rangeSo):
765
    prec = c_int(0)
766
    retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
767
    if retc == 0:
768
        return(None)
769
    return(int(prec.value))
770
# End pobyso_get_prec_of_range_so_sa()
771

  
772 835
def pobyso_guess_degree_sa_sa(functionSa, intervalSa, approxErrorSa, 
773 836
                              weightSa=None, degreeBoundSa=None):
774 837
    """
......
923 986
    """
924 987
    return(sollya_lib_parse_string(string))
925 988

  
989
def pobyso_precision_so_sa(ctExpSo):
990
    precisionSo = sollya_lib_precision(ctExpSo)
991
    precisionSa = pobyso_constant_from_int_so_sa(precisionSo)
992
    sollya_lib_clear_obj(precisionSo)
993
    return precisionSa
994
# End pobyso_precision_so_sa
995
    
926 996
def pobyso_range(rnLowerBound, rnUpperBound):
927 997
    """ Legacy function. See pobyso_range_sa_so. """
928 998
    return(pobyso_range_sa_so(rnLowerBound, rnUpperBound)) 
......
997 1067
                                        upperBound, \
998 1068
                                        weight, \
999 1069
                                        quality))
1070
# End pobyso_remez_canonical.
1071

  
1000 1072
def pobyso_remez_canonical_sa_so(func, \
1001 1073
                                 degree, \
1002 1074
                                 lowerBound, \
......
1075 1147
    if not sollya_lib_obj_is_function(funcSo):
1076 1148
        return(None)
1077 1149
    return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None))
1078
    
1150
# End pobyso_remez_canonical_so_so.
1151

  
1079 1152
def pobyso_set_canonical_off():
1080 1153
    sollya_lib_set_canonical(sollya_lib_off())
1081 1154

  

Formats disponibles : Unified diff