@@ -63,6 +63,33 @@ struct _UnivariateOperator{F,F′,F′′}
63
63
end
64
64
end
65
65
66
+ function eval_univariate_function (operator:: _UnivariateOperator , x:: T ) where {T}
67
+ ret = operator. f (x)
68
+ check_return_type (T, ret)
69
+ return ret:: T
70
+ end
71
+
72
+ function eval_univariate_gradient (operator:: _UnivariateOperator , x:: T ) where {T}
73
+ ret = operator. f′ (x)
74
+ check_return_type (T, ret)
75
+ return ret:: T
76
+ end
77
+
78
+ function eval_univariate_hessian (operator:: _UnivariateOperator , x:: T ) where {T}
79
+ ret = operator. f′′ (x)
80
+ check_return_type (T, ret)
81
+ return ret:: T
82
+ end
83
+
84
+ function eval_univariate_function_and_gradient (
85
+ operator:: _UnivariateOperator ,
86
+ x:: T ,
87
+ ) where {T}
88
+ ret_f = eval_univariate_function (operator, x)
89
+ ret_f′ = eval_univariate_gradient (operator, x)
90
+ return ret_f, ret_f′
91
+ end
92
+
66
93
struct _MultivariateOperator{F,F′,F′′}
67
94
N:: Int
68
95
f:: F
@@ -517,81 +544,214 @@ end
517
544
"""
518
545
eval_univariate_function(
519
546
registry::OperatorRegistry,
520
- op::Symbol,
547
+ op::Union{ Symbol,Integer} ,
521
548
x::T,
522
549
) where {T}
523
550
524
551
Evaluate the operator `op(x)::T`, where `op` is a univariate function in
525
552
`registry`.
553
+
554
+ If `op isa Integer`, then `op` is the index in
555
+ `registry.univariate_operators[op]`.
556
+
557
+ ## Example
558
+
559
+ ```jldoctest
560
+ julia> import MathOptInterface as MOI
561
+
562
+ julia> r = MOI.Nonlinear.OperatorRegistry();
563
+
564
+ julia> MOI.Nonlinear.eval_univariate_function(r, :abs, -1.2)
565
+ 1.2
566
+
567
+ julia> r.univariate_operators[3]
568
+ :abs
569
+
570
+ julia> MOI.Nonlinear.eval_univariate_function(r, 3, -1.2)
571
+ 1.2
572
+ ```
526
573
"""
527
574
function eval_univariate_function (
528
575
registry:: OperatorRegistry ,
529
576
op:: Symbol ,
530
577
x:: T ,
531
578
) where {T}
532
579
id = registry. univariate_operator_to_id[op]
580
+ return eval_univariate_function (registry, id, x)
581
+ end
582
+
583
+ function eval_univariate_function (
584
+ registry:: OperatorRegistry ,
585
+ id:: Integer ,
586
+ x:: T ,
587
+ ) where {T}
533
588
if id <= registry. univariate_user_operator_start
534
589
f, _ = _eval_univariate (id, x)
535
590
return f:: T
536
591
end
537
592
offset = id - registry. univariate_user_operator_start
538
593
operator = registry. registered_univariate_operators[offset]
539
- ret = operator. f (x)
540
- check_return_type (T, ret)
541
- return ret:: T
594
+ return eval_univariate_function (operator, x)
542
595
end
543
596
544
597
"""
545
598
eval_univariate_gradient(
546
599
registry::OperatorRegistry,
547
- op::Symbol,
600
+ op::Union{ Symbol,Integer} ,
548
601
x::T,
549
602
) where {T}
550
603
551
604
Evaluate the first-derivative of the operator `op(x)::T`, where `op` is a
552
605
univariate function in `registry`.
606
+
607
+ If `op isa Integer`, then `op` is the index in
608
+ `registry.univariate_operators[op]`.
609
+
610
+ ## Example
611
+
612
+ ```jldoctest
613
+ julia> import MathOptInterface as MOI
614
+
615
+ julia> r = MOI.Nonlinear.OperatorRegistry();
616
+
617
+ julia> MOI.Nonlinear.eval_univariate_gradient(r, :abs, -1.2)
618
+ -1.0
619
+
620
+ julia> r.univariate_operators[3]
621
+ :abs
622
+
623
+ julia> MOI.Nonlinear.eval_univariate_gradient(r, 3, -1.2)
624
+ -1.0
625
+ ```
553
626
"""
554
627
function eval_univariate_gradient (
555
628
registry:: OperatorRegistry ,
556
629
op:: Symbol ,
557
630
x:: T ,
558
631
) where {T}
559
632
id = registry. univariate_operator_to_id[op]
633
+ return eval_univariate_gradient (registry, id, x)
634
+ end
635
+
636
+ function eval_univariate_gradient (
637
+ registry:: OperatorRegistry ,
638
+ id:: Integer ,
639
+ x:: T ,
640
+ ) where {T}
560
641
if id <= registry. univariate_user_operator_start
561
642
_, f′ = _eval_univariate (id, x)
562
643
return f′:: T
563
644
end
564
645
offset = id - registry. univariate_user_operator_start
565
646
operator = registry. registered_univariate_operators[offset]
566
- ret = operator. f′ (x)
567
- check_return_type (T, ret)
568
- return ret:: T
647
+ return eval_univariate_gradient (operator, x)
648
+ end
649
+
650
+ """
651
+ eval_univariate_function_and_gradient(
652
+ registry::OperatorRegistry,
653
+ op::Union{Symbol,Integer},
654
+ x::T,
655
+ )::Tuple{T,T} where {T}
656
+
657
+ Evaluate the function and first-derivative of the operator `op(x)::T`, where
658
+ `op` is a univariate function in `registry`.
659
+
660
+ If `op isa Integer`, then `op` is the index in
661
+ `registry.univariate_operators[op]`.
662
+
663
+ ## Example
664
+
665
+ ```jldoctest
666
+ julia> import MathOptInterface as MOI
667
+
668
+ julia> r = MOI.Nonlinear.OperatorRegistry();
669
+
670
+ julia> MOI.Nonlinear.eval_univariate_function_and_gradient(r, :abs, -1.2)
671
+ (1.2, -1.0)
672
+
673
+ julia> r.univariate_operators[3]
674
+ :abs
675
+
676
+ julia> MOI.Nonlinear.eval_univariate_function_and_gradient(r, 3, -1.2)
677
+ (1.2, -1.0)
678
+ ```
679
+ """
680
+ function eval_univariate_function_and_gradient (
681
+ registry:: OperatorRegistry ,
682
+ op:: Symbol ,
683
+ x:: T ,
684
+ ) where {T}
685
+ id = registry. univariate_operator_to_id[op]
686
+ return eval_univariate_function_and_gradient (registry, id, x)
687
+ end
688
+
689
+ function eval_univariate_function_and_gradient (
690
+ registry:: OperatorRegistry ,
691
+ id:: Integer ,
692
+ x:: T ,
693
+ ) where {T}
694
+ if id <= registry. univariate_user_operator_start
695
+ return _eval_univariate (id, x):: Tuple{T,T}
696
+ end
697
+ offset = id - registry. univariate_user_operator_start
698
+ operator = registry. registered_univariate_operators[offset]
699
+ return eval_univariate_function_and_gradient (operator, x)
569
700
end
570
701
571
702
"""
572
703
eval_univariate_hessian(
573
704
registry::OperatorRegistry,
574
- op::Symbol,
705
+ op::Union{ Symbol,Integer} ,
575
706
x::T,
576
707
) where {T}
577
708
578
709
Evaluate the second-derivative of the operator `op(x)::T`, where `op` is a
579
710
univariate function in `registry`.
711
+
712
+ If `op isa Integer`, then `op` is the index in
713
+ `registry.univariate_operators[op]`.
714
+
715
+ ## Example
716
+
717
+ ```jldoctest
718
+ julia> import MathOptInterface as MOI
719
+
720
+ julia> r = MOI.Nonlinear.OperatorRegistry();
721
+
722
+ julia> MOI.Nonlinear.eval_univariate_hessian(r, :sin, 1.0)
723
+ -0.8414709848078965
724
+
725
+ julia> r.univariate_operators[16]
726
+ :sin
727
+
728
+ julia> MOI.Nonlinear.eval_univariate_hessian(r, 16, 1.0)
729
+ -0.8414709848078965
730
+
731
+ julia> -sin(1.0)
732
+ -0.8414709848078965
733
+ ```
580
734
"""
581
735
function eval_univariate_hessian (
582
736
registry:: OperatorRegistry ,
583
737
op:: Symbol ,
584
738
x:: T ,
585
739
) where {T}
586
740
id = registry. univariate_operator_to_id[op]
741
+ return eval_univariate_hessian (registry, id, x)
742
+ end
743
+
744
+ function eval_univariate_hessian (
745
+ registry:: OperatorRegistry ,
746
+ id:: Integer ,
747
+ x:: T ,
748
+ ) where {T}
587
749
if id <= registry. univariate_user_operator_start
588
750
return _eval_univariate_2nd_deriv (id, x):: T
589
751
end
590
752
offset = id - registry. univariate_user_operator_start
591
753
operator = registry. registered_univariate_operators[offset]
592
- ret = operator. f′′ (x)
593
- check_return_type (T, ret)
594
- return ret:: T
754
+ return eval_univariate_hessian (operator, x)
595
755
end
596
756
597
757
"""
0 commit comments