Révision 83 pobysoPythonSage/src/pobyso.py

pobyso.py (revision 83)
115 115
    #print "rnDummy: ", rnDummy
116 116
    # Compare the local Sage RealNumber with rnArg.
117 117
    return(cmp_rn_value(rnArg, rnLocal))
118
# End pobyso_smp
118 119

  
119 120
def pobyso_change_var_in_function_so_so(funcSo, chvarExpSo):
121
    """
122
    Variable change in a function.
123
    """
120 124
    return(sollya_lib_evaluate(funcSo,chvarExpSo))
121
    
125
# End pobyso_change_var_in_function_so_so     
122 126

  
123 127
def pobyso_chebyshevform_so_so(functionSo, degreeSo, intervalSo):
124 128
    resultSo = sollya_lib_chebyshevform(functionSo, degreeSo, intervalSo)
125 129
    return(resultSo)
126
        
130
# End pobyso_chebyshevform_so_so.
127 131

  
128

  
129 132
def pobyso_compute_pos_function_abs_val_bounds_sa_sa(funcSa, lowerBoundSa, \
130 133
                                                     upperBoundSa):
131 134
    """
......
134 137
    funcSo = pobyso_parse_string(funcSa._assume_str())
135 138
    rangeSo = pobyso_range_sa_so(lowerBoundSa, upperBoundSa)
136 139
    infnormSo = pobyso_infnorm_so_so(funcSo,rangeSo)
140
    # Sollya return the infnorm as an interval.
137 141
    fMaxSa = pobyso_get_interval_from_range_so_sa(infnormSo)
138 142
    # Get the top bound and compute the binade top limit.
139 143
    fMaxUpperBoundSa = fMaxSa.upper()
......
142 146
    funcAuxSo = pobyso_parse_string(str(binadeTopLimitSa) +  \
143 147
                                    '-(' + f._assume_str() + ')')
144 148
    pobyso_autoprint(funcAuxSo)
145
    # Clear the Sollay range before a new call to infnorm and issue the call.
149
    # Clear the Sollya range before a new call to infnorm and issue the call.
146 150
    sollya_lib_clear_obj(infnormSo)
147 151
    infnormSo = pobyso_infnorm_so_so(funcAuxSo,rangeSo)
148 152
    fMinSa = pobyso_get_interval_from_range_so_sa(infnormSo)
......
154 158
    # Create a RealIntervalField and create an interval with the "good" bounds.
155 159
    RRRI = RealIntervalField(maxPrecSa)
156 160
    imageIntervalSa = RRRI(fMinLowerBoundSa, fMaxUpperBoundSa)
157
    # Free the uneeded Sollya objects
161
    # Free the unneeded Sollya objects
158 162
    sollya_lib_clear_obj(funcSo)
159 163
    sollya_lib_clear_obj(funcAuxSo)
160 164
    sollya_lib_clear_obj(rangeSo)
161 165
    return(imageIntervalSa)
162
    # End pobyso_compute_function_abs_val_bounds_sa_sa
166
# End pobyso_compute_pos_function_abs_val_bounds_sa_sa
163 167

  
164 168
def pobyso_constant(rnArg):
165 169
    """ Legacy function. See pobyso_constant_sa_so. """
......
301 305
    return(sollya_lib_get_constant(get_rn_value(rnArg), soConst))
302 306
    
303 307
def pobyso_get_constant_as_rn(ctExpSo):
304
    """ Legacy function. See pobyso_get_constant_as_rn_so_sa. """ 
308
    """ 
309
    Legacy function. See pobyso_get_constant_as_rn_so_sa. 
310
    """ 
305 311
    return(pobyso_get_constant_as_rn_so_sa(ctExpSo))
306 312
    
307 313
def pobyso_get_constant_as_rn_so_sa(constExpSo):
314
    """
315
    Get a Sollya constant as a Sage "real number".
316
    The precision of the floating-point number returned is that of the Sollya
317
    constant.
318
    """
308 319
    precisionSa  = pobyso_get_prec_of_constant_so_sa(constExpSo) 
309 320
    RRRR = RealField(precisionSa)
310 321
    rnSa = RRRR(0)
311 322
    sollya_lib_get_constant(get_rn_value(rnSa), constExpSo)
312 323
    return(rnSa)
324
# End pobyso_get_constant_as_rn_so_sa
313 325

  
314 326
def pobyso_get_constant_as_rn_with_rf(ctExp, realField):
315
    """ Legacy function. See pobyso_get_constant_as_rn_with_rf_so_sa."""
327
    """ 
328
    Legacy function. See pobyso_get_constant_as_rn_with_rf_so_sa.
329
    """
316 330
    return(pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField))
317 331
    
318 332
def pobyso_get_constant_as_rn_with_rf_so_sa(ctExpSo, realFieldSa = None):
333
    """
334
    Get a Sollya constant as a Sage "real number".
335
    If no real field is specified, the precision of the floating-point number 
336
    returned is that of the Solly constant.
337
    Otherwise is is that of the real field. Hence rounding may happen.
338
    """
319 339
    if realFieldSa is None:
320 340
        sollyaPrecSa = pobyso_get_prec_so_sa()
321 341
        realFieldSa = RealField(sollyaPrecSa)
322 342
    rnSa = realFieldSa(0)
323 343
    sollya_lib_get_constant(get_rn_value(rnSa), ctExpSo)
324 344
    return(rnSa)
345
# End pobyso_get_constant_as_rn_with_rf_so_sa
325 346

  
326 347
def pobyso_get_free_variable_name():
327
    """ Legacy function. See pobyso_get_free_variable_name_so_sa."""
348
    """ 
349
    Legacy function. See pobyso_get_free_variable_name_so_sa.
350
    """
328 351
    return(pobyso_get_free_variable_name_so_sa())
329 352

  
330 353
def pobyso_get_free_variable_name_so_sa():
331 354
    return(sollya_lib_get_free_variable_name())
332 355
    
333 356
def pobyso_get_function_arity(expressionSo):
334
    """ Legacy function. See pobyso_get_function_arity_so_sa."""
357
    """ 
358
    Legacy function. See pobyso_get_function_arity_so_sa.
359
    """
335 360
    return(pobyso_get_function_arity_so_sa(expressionSo))
336 361

  
337 362
def pobyso_get_function_arity_so_sa(expressionSo):
......
340 365
    return(int(arity.value))
341 366

  
342 367
def pobyso_get_head_function(expressionSo):
343
    """ Legacy function. See pobyso_get_head_function_so_sa. """
368
    """ 
369
    Legacy function. See pobyso_get_head_function_so_sa. 
370
    """
344 371
    return(pobyso_get_head_function_so_sa(expressionSo)) 
345 372

  
346 373
def pobyso_get_head_function_so_sa(expressionSo):
......
351 378
def pobyso_get_interval_from_range_so_sa(soRange, realIntervalFieldSa = None ):
352 379
    """
353 380
    Return the Sage interval corresponding to the Sollya range argument.
354
    If no reaInterval lField is passed as argument, the interval bounds are not
381
    If no reaIntervalField is passed as an argument, the interval bounds are not
355 382
    rounded: they are elements of RealIntervalField of the "right" precision
356 383
    to hold all the digits.
357 384
    """
......
456 483
    """
457 484
    #pobyso_autoprint(sollyaExp)
458 485
    operator = pobyso_get_head_function_so_sa(sollyaExp)
486
    sollyaLibFreeVariableName = sollya_lib_get_free_variable_name()
459 487
    # Constants and the free variable are special cases.
460 488
    # All other operator are dealt with in the same way.
461 489
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
......
487 515
        return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExp, realField)
488 516
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
489 517
        #print "This is free variable"
490
        return(eval(sollya_lib_get_free_variable_name()))
518
        return(eval(sollyaLibFreeVariableName))
491 519
    else:
492 520
        print "Unexpected"
493 521
        return eval('None')
494 522
# End pobyso_get_sage_poly_from_sollya_poly
495 523

  
496 524
def pobyso_get_poly_sa_so(polySo, realFieldSa=None):
525
    """
526
    Create a Sollya polynomial from a Sage polynomial.
527
    """
497 528
    pass
498 529
# pobyso_get_poly_sa_so
499 530

  
......
537 568
    Get the subfunctions of an expression.
538 569
    Return the number of subfunctions and the list of subfunctions addresses.
539 570
    S.T.: Could not figure out another way than that ugly list of declarations
540
    to recover the addresses of the subfunctions. 
571
    to recover the addresses of the subfunctions.
572
    We limit ourselves to arity 8 functions. 
541 573
    """
542 574
    subf0 = c_int(0)
543 575
    subf1 = c_int(0)
......
551 583
    arity = c_int(0)
552 584
    nullPtr = POINTER(c_int)()
553 585
    sollya_lib_get_subfunctions(expressionSo, byref(arity), \
554
    byref(subf0), byref(subf1), byref(subf2), byref(subf3), byref(subf4), byref(subf5),\
555
     byref(subf6), byref(subf7), byref(subf8), nullPtr, None) 
556
#    byref(cast(subfunctions[0], POINTER(c_int))), byref(cast(subfunctions[0], POINTER(c_int))), \
557
#    byref(cast(subfunctions[2], POINTER(c_int))), byref(cast(subfunctions[3], POINTER(c_int))), \
558
#    byref(cast(subfunctions[4], POINTER(c_int))), byref(cast(subfunctions[5], POINTER(c_int))), \
559
#    byref(cast(subfunctions[6], POINTER(c_int))), byref(cast(subfunctions[7], POINTER(c_int))), \
586
      byref(subf0), byref(subf1), byref(subf2), byref(subf3), \
587
      byref(subf4), byref(subf5),\
588
      byref(subf6), byref(subf7), byref(subf8), nullPtr, None) 
589
#    byref(cast(subfunctions[0], POINTER(c_int))), \
590
#    byref(cast(subfunctions[0], POINTER(c_int))), \
591
#    byref(cast(subfunctions[2], POINTER(c_int))), \
592
#    byref(cast(subfunctions[3], POINTER(c_int))), \
593
#    byref(cast(subfunctions[4], POINTER(c_int))), \
594
#    byref(cast(subfunctions[5], POINTER(c_int))), \
595
#    byref(cast(subfunctions[6], POINTER(c_int))), \
596
#    byref(cast(subfunctions[7], POINTER(c_int))), \
560 597
#    byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
561
    subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, subf8]
598
    subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, \
599
                    subf8]
562 600
    subs = []
563 601
    if arity.value > pobyso_max_arity:
564 602
        return(0,[])
......
581 619
    sollya_lib_get_constant_as_int(byref(precSa), precSo)
582 620
    sollya_lib_clear_obj(precSo)
583 621
    return(int(precSa.value))
622
# End pobyso_get_prec_so_sa.
584 623

  
585 624
def pobyso_get_prec_of_constant(ctExpSo):
586 625
    """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
......
612 651
    pobyso_name_free_variable_sa_so(freeVariableName)
613 652

  
614 653
def pobyso_name_free_variable_sa_so(freeVariableName):
654
    """
655
    Set the free variable name in Sollya from a Sage string.
656
    """
615 657
    sollya_lib_name_free_variable(freeVariableName)
616 658

  
617 659
def pobyso_parse_string(string):
......
619 661
    return(pobyso_parse_string_sa_so(string))
620 662
 
621 663
def pobyso_parse_string_sa_so(string):
664
    """
665
    Get the Sollya expression computed from a Sage string.
666
    """
622 667
    return(sollya_lib_parse_string(string))
623 668

  
624 669
def pobyso_range(rnLowerBound, rnUpperBound):
......
627 672

  
628 673
def pobyso_bounds_to_range_sa_so(rnLowerBoundSa, rnUpperBoundSa):
629 674
    """
630
    Return a Sollya range from to 2 RealField elements.
675
    Return a Sollya range from to 2 RealField Sage elements.
631 676
    The Sollya range element has a sufficient precision to hold all
632
    the digits of the bounds.
677
    the digits of the Sage bounds.
633 678
    """
679
    # Sanity check.
634 680
    if rnLowerBoundSa > rnUpperBoundSa:
635 681
        return None
682
    # Check for the largest precision.
636 683
    lbPrec = rnLowerBoundSa.parent().precision()
637 684
    ubPrec = rnLowerBoundSa.parent().precision()
638 685
    currentSollyaPrecSa = pobyso_get_prec_so_sa()
......
645 692
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa))
646 693
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBoundSa))
647 694
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
648
    currentPrecSo = sollya_lib_get_prec(None)
649 695
    if maxPrecSa > currentSollyaPrecSa:
650 696
        sollya_lib_set_prec(currentPrecSo)
651 697
        sollya_lib_clear_obj(currentPrecSo)
......
655 701
    return(rangeSo)
656 702

  
657 703
def pobyso_range_to_interval_so_sa(rangeSo, realIntervalField = None):
704
    """
705
    Get a Sage interval from a Sollya range.
706
    If no realIntervalField is given as a parameter, the Sage interval
707
    precision is that of the Sollya range.
708
    Otherwise, the precision is that of the realIntervalField. Rounding
709
    may happen.
710
    """
658 711
    if realIntervalField is None:
659 712
        precSa = pobyso_get_prec_of_range_so_sa(rangeSo)
660 713
        realIntervalField = RealIntervalField(precSa)
......
672 725
    All arguments are Sage/Python.
673 726
    The functions (func and weight) must be passed as expressions or strings.
674 727
    Otherwise the function fails. 
675
    The return value is a pointer is a Sage polynomial.
728
    The return value is a Sage polynomial.
676 729
    """
677
    var('zorglub')    # Dummy variable name for type check only.
730
    var('zorglub')    # Dummy variable name for type check only. Type of 
731
    # zorglub is "symbolic expression".
678 732
    polySo = pobyso_remez_canonical_sa_so(func, \
679 733
                                 degree, \
680 734
                                 lowerBound, \
681 735
                                 upperBound, \
682 736
                                 weight = None, \
683 737
                                 quality = None)
738
    # String test
684 739
    if parent(func) == parent("string"):
685 740
        functionSa = eval(func)
686 741
    # Expression test.
687 742
    elif type(func) == type(zorglub):
688 743
        functionSa = func
744
    else:
745
        return None
746
    #
689 747
    maxPrecision = 0
690 748
    if polySo is None:
691 749
        return(None)
......
694 752
    polynomialRing = RRRR[functionSa.variables()[0]]
695 753
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRR)
696 754
    polySa = polynomial(expSa, polynomialRing)
755
    sollya_lib_clear_obj(polySo)
697 756
    return(polySa)
698 757
    
699 758
def pobyso_remez_canonical(func, \
......
721 780
    Otherwise the function fails. 
722 781
    The return value is a pointer to a Sollya function.
723 782
    """
724
    var('zorglub')    # Dummy variable name for type check only.
783
    var('zorglub')    # Dummy variable name for type check only. Type of
784
    # zorglub is "symbolic expression".
725 785
    currentVariableName = None
726 786
    # The func argument can be of different types (string, 
727 787
    # symbolic expression...)
......
750 810
        qualitySo= pobyso_constant_sa_so(quality)
751 811
    else:
752 812
        qualitySo = None
753
    return(sollya_lib_remez(functionSo, \
754
                            degreeSo, \
755
                            rangeSo, \
756
                            weightSo, \
757
                            qualitySo, \
758
                            None))
759
    
813
        
814
    remezPolySo = sollya_lib_remez(functionSo, \
815
                                   degreeSo, \
816
                                   rangeSo, \
817
                                   weightSo, \
818
                                   qualitySo, \
819
                                   None)
820
    sollya_lib_clear_obj(functionSo)
821
    sollya_lib_clear_obj(degreeSo)
822
    sollya_lib_clear_obj(rangeSo)
823
    sollya_lib_clear_obj(weightSo)
824
    if not qualitySo is None:
825
        sollya_lib_clear_obj(qualtiySo)
826
    return(remezPolySo)
827
# End pobyso_remez_canonical_sa_so
828

  
760 829
def pobyso_remez_canonical_so_so(funcSo, \
761 830
                                 degreeSo, \
762 831
                                 rangeSo, \
......
817 886
    # If changed, reset the Sollya working precision.
818 887
    if not sollyaPrecSo is None:
819 888
        sollya_lib_set_prec(initialPrecSo)
820
        sollya_lib_clear_obj(initailPrecSo)
889
        sollya_lib_clear_obj(initialPrecSo)
821 890
    return((polyVarChangedSo, intervalCenterSo, maxErrorSo))
822 891
# end pobyso_taylor_expansion_with_change_var_so_so
823 892

  
......
870 939
    for 'interval' with 'errorType'. 
871 940
    point: must be a Real or a Real interval.
872 941
    return the Taylor form as an array
873
    TODO: take care of the interval and of point when it is an interval;
942
    TODO: take care of the interval and of the point when it is an interval;
874 943
          when errorType is not None;
875
          take care of the other elements of the Taylor form (coefficients errors and
876
          delta.
944
          take care of the other elements of the Taylor form (coefficients 
945
          errors and delta.
877 946
    """
878 947
    # Absolute as the default error.
879 948
    if errorType is None:
......
902 971
        # TODO: deal with the interval case.
903 972
        pass
904 973
    # Call Sollya
905
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\
974
    taylorFormSo = \
975
        sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\
906 976
                                         None)
907 977
    (tfsAsList, numElements, isEndElliptic) = \
908 978
            pobyso_get_list_elements_so_so(taylorFormSo)

Formats disponibles : Unified diff