@@ -63,6 +63,33 @@ struct _UnivariateOperator{F,F′,F′′}
6363 end
6464end
6565
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+
6693struct _MultivariateOperator{F,F′,F′′}
6794 N:: Int
6895 f:: F
@@ -517,81 +544,214 @@ end
517544"""
518545 eval_univariate_function(
519546 registry::OperatorRegistry,
520- op::Symbol,
547+ op::Union{ Symbol,Integer} ,
521548 x::T,
522549 ) where {T}
523550
524551Evaluate the operator `op(x)::T`, where `op` is a univariate function in
525552`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+ ```
526573"""
527574function eval_univariate_function (
528575 registry:: OperatorRegistry ,
529576 op:: Symbol ,
530577 x:: T ,
531578) where {T}
532579 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}
533588 if id <= registry. univariate_user_operator_start
534589 f, _ = _eval_univariate (id, x)
535590 return f:: T
536591 end
537592 offset = id - registry. univariate_user_operator_start
538593 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)
542595end
543596
544597"""
545598 eval_univariate_gradient(
546599 registry::OperatorRegistry,
547- op::Symbol,
600+ op::Union{ Symbol,Integer} ,
548601 x::T,
549602 ) where {T}
550603
551604Evaluate the first-derivative of the operator `op(x)::T`, where `op` is a
552605univariate 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+ ```
553626"""
554627function eval_univariate_gradient (
555628 registry:: OperatorRegistry ,
556629 op:: Symbol ,
557630 x:: T ,
558631) where {T}
559632 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}
560641 if id <= registry. univariate_user_operator_start
561642 _, f′ = _eval_univariate (id, x)
562643 return f′:: T
563644 end
564645 offset = id - registry. univariate_user_operator_start
565646 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)
569700end
570701
571702"""
572703 eval_univariate_hessian(
573704 registry::OperatorRegistry,
574- op::Symbol,
705+ op::Union{ Symbol,Integer} ,
575706 x::T,
576707 ) where {T}
577708
578709Evaluate the second-derivative of the operator `op(x)::T`, where `op` is a
579710univariate 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+ ```
580734"""
581735function eval_univariate_hessian (
582736 registry:: OperatorRegistry ,
583737 op:: Symbol ,
584738 x:: T ,
585739) where {T}
586740 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}
587749 if id <= registry. univariate_user_operator_start
588750 return _eval_univariate_2nd_deriv (id, x):: T
589751 end
590752 offset = id - registry. univariate_user_operator_start
591753 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)
595755end
596756
597757"""
0 commit comments