NAG Library Function Document nag_search_vector (m01fsc)

Similar documents
NAG Library Function Document nag_stable_sort (m01ctc)

NAG Library Function Document nag_chain_sort (m01cuc)

NAG Library Function Document nag_durbin_watson_stat (g02fcc)

NAG Library Function Document nag_friedman_test (g08aec)

NAG Library Function Document nag_quad_md_gauss (d01fbc)

NAG Library Function Document nag_opt_check_deriv (e04hcc)

NAG Library Function Document nag_tsa_dickey_fuller_unit (g13awc)

NAG Library Function Document nag_regress_confid_interval (g02cbc)

NAG Library Function Document nag_legendre_p (s22aac)

NAG Library Function Document nag_1d_spline_evaluate (e02bbc)

NAG Library Function Document nag_regsn_mult_linear_delete_var (g02dfc)

NAG Library Function Document nag_2d_spline_eval (e02dec)

NAG Library Function Document nag_corr_cov (g02bxc)

NAG Library Function Document nag_mv_prin_comp (g03aac)

NAG Library Function Document nag_frequency_table (g01aec)

NAG Library Function Document nag_dger (f16pmc)

NAG Library Function Document nag_2d_panel_sort (e02zac)

NAG Library Function Document nag_kruskal_wallis_test (g08afc)

NAG Library Function Document nag_sum_convcorr_real (c06fkc)

NAG Library Function Document nag_prob_durbin_watson (g01epc)

NAG Library Function Document nag_real_gen_matrix_exp (f01ecc)

NAG Library Function Document nag_1d_spline_deriv (e02bcc)

NAG Library Function Document nag_matop_complex_gen_matrix_fun_num (f01flc)

NAG Library Function Document nag_dtpttr (f01vcc)

NAG Library Function Document nag_regsn_mult_linear_addrem_obs (g02dcc)

NAG Library Function Document nag_tsa_auto_corr_part (g13acc)

NAG Library Function Document nag_summary_stats_onevar_combine (g01auc)

NAG Library Function Document. nag_matop_real_gen_matrix_cond_num

NAG Library Function Document nag_regsn_mult_linear_upd_model (g02ddc)

NAG Library Function Document nag_2d_spline_eval_rect (e02dfc)

NAG Library Function Document nag_dspr (f16pqc)

NAG Library Function Document nag_zsymv (f16tac)

NAG Library Function Document nag_matop_real_gen_matrix_fun_num (f01elc)

NAG Library Function Document. nag_matop_complex_gen_matrix_cond_num

NAG Library Function Document nag_5d_shep_eval (e01tnc)

NAG Library Function Document nag_dummy_vars (g04eac)

NAG Library Function Document nag_bessel_j0 (s17aec)

NAG Library Function Document nag_dgb_norm (f16rbc)

NAG Library Function Document nag_dtfttp (f01vlc)

NAG Library Function Document nag_smooth_spline_fit (g10abc)

NAG Library Function Document nag_tsa_cp_binary (g13ndc)

NAG Library Function Document nag_ode_bvp_ps_lin_cheb_eval (d02uzc)

NAG Library Function Document nag_dtrttf (f01vec)

NAG Library Function Document nag_dtpsv (f16plc)

NAG Library Function Document nag_wfilt (c09aac)

NAG Library Function Document nag_outlier_peirce (g07gac)

NAG Library Function Document nag_zaxpby (f16gcc)

NAG Library Function Document nag_ode_ivp_rk_interp (d02pxc)

NAG Library Function Document nag_bsm_price (s30aac)

NAG Library Function Document nag_opt_one_var_deriv (e04bbc)

NAG Library Function Document nag_dsf_norm (f16rkc)

NAG Library Function Document nag_prob_non_central_f_dist (g01gdc)

NAG Library Function Document nag_dwt (c09cac)

NAG Library Function Document nag_bessel_j1 (s17afc)

NAG Library Function Document nag_dgbmv (f16pbc)

NAG C Library Function Document nag_binary_factor_service (g11sbc)

NAG Library Function Document nag_chi_sq_2_way_table (g11aac)

NAG Library Function Document nag_multid_quad_monte_carlo_1 (d01xbc)

NAG Library Function Document nag_zhpr2 (f16ssc)

NAG Library Function Document nag_quad_2d_fin (d01dac)

NAG Library Function Document nag_sum_fft_cosine (c06rfc)

NAG Library Function Document nag_mv_discrim (g03dac)

NAG Library Function Document nag_tsa_noise_spectrum_bivar (g13cgc)

NAG Library Function Document nag_sum_sqs_update (g02btc)

NAG Library Function Document nag_robust_m_corr_user_fn (g02hlc)

NAG Library Function Document nag_binomial_ci (g07aac)

NAG Library Function Document nag_4d_shep_eval (e01tlc)

NAG Library Function Document nag_superlu_lu_factorize (f11mec)

NAG Library Function Document nag_dgelsd (f08kcc)

NAG Library Function Document nag_mv_dendrogram (g03ehc)

NAG Library Function Document nag_dgetrf (f07adc)

NAG Library Function Document nag_ztfttp (f01vmc)

NAG Library Function Document nag_1d_spline_fit (e02bec)

NAG Library Function Document nag_complex_bessel_j (s17dec)

NAG Library Function Document nag_kalman_sqrt_filt_info_var (g13ecc)

NAG Library Function Document nag_real_cholesky_skyline_solve (f04mcc)

NAG Library Function Document nag_robust_m_corr_user_fn_no_derr (g02hmc)

NAG Library Function Document nag_zhpev (f08gnc)

NAG Library Function Document nag_opt_simplex_easy (e04cbc)

NAG Library Function Document nag_tsa_spectrum_univar_cov (g13cac)

NAG Library Function Document nag_complex_bessel_i (s18dec)

NAG Library Function Document nag_exp_integral (s13aac)

NAG Library Function Document nag_ztrttf (f01vfc)

NAG Library Function Document nag_complex_bessel_y (s17dcc)

NAG Library Function Document nag_sum_fft_real_3d (c06pyc)

NAG Library Function Document nag_tsa_varma_update (g13dkc)

NAG Library Function Document nag_zhfrk (f16zqc)

NAG Library Function Document nag_dstev (f08jac)

NAG Library Function Document nag_dgges (f08xac)

NAG Library Function Document nag_ode_bvp_fd_nonlin_fixedbc (d02gac)

NAG Library Function Document nag_1d_spline_fit_knots (e02bac)

NAG C Library Function Document nag_multid_quad_monte_carlo_1 (d01xbc)

NAG Library Function Document nag_mldwt_2d (c09ecc)

NAG Library Function Document nag_zgelss (f08knc)

NAG Library Function Document nag_zero_nonlin_eqns_deriv_1 (c05ubc)

NAG Library Function Document nag_dgttrs (f07cec)

NAG Library Function Document nag_dtrmv (f16pfc)

NAG Library Function Document nag_zgelsy (f08bnc)

NAG Library Function Document nag_zero_nonlin_eqns_rcomm (c05qdc)

NAG Library Function Document nag_matop_complex_gen_matrix_cond_sqrt (f01kdc)

Transcription:

NAG Library Function Document nag_search_vector () 1 Purpose nag_search_vector () searches a vector of arbitrary type data objects for the first or last match to a given value. 2 Specification #include <nag.h> #include <nagm01.h> Nag_Boolean nag_search_vector (Pointer key, const Pointer vec, size_t n, ptrdiff_t stride, Integer (compare)(const Nag_Pointer a, const Nag_Pointer b), Nag_SortOrder order, Nag_SearchMatch final, Pointer match, NagError fail) 3 Description nag_search_vector () searches a sorted vector of n arbitrary type data objects, which are stored in the elements of an array at intervals of length stride. vec must have previously been sorted into the specified order. The function searches for the first or last match depending on the value of final. It returns Nag_TRUE if an exact match is found and match is set to point at that object. If there is no exact match then Nag_FALSE is returned and match is set to point to either the next later element, if final ¼ Nag First, or the next earlier element, if final ¼ Nag Last. 4 References None. 5 Arguments 1: key Pointer Input On entry: the object to search for. 2: vec½nš const Pointer Input On entry: the array of objects to be searched. 3: n size_t Input On entry: the number n of objects to be searched. Constraint: n 0. 4: stride ptrdiff_t Input On entry: the increment between data items in vec to be searched. Note: ifstride is positive, vec should point at the first data object; otherwise vec should point at the last data object..1

NAG Library Manual It should be noted that jstridej must be greater than or equal to size_of (data objects), for the search to be performed successfully. However, the code performs no check for violation of this constraint. Constraint: jstridej > 0. 5: compare function, supplied by the user External Function nag_search_vector () compares two data objects. If its arguments are pointers to a structure, this function must allow for the offset of the data field in the structure (if it is not the first). The function must return: 1 if the first data field is less than the second, 0 if the first data field is equal to the second, 1 if the first data field is greater than the second. The specification of compare is: Integer compare (const Nag_Pointer a, const Nag_Pointer b) 1: a const Nag_Pointer Input On entry: the first data field. 2: b const Nag_Pointer Input On entry: the second data field. 6: order Nag_SortOrder Input On entry: specifies whether the array will be sorted into ascending or descending order. Constraint: order ¼ Nag Ascending or Nag Descending. 7: final Nag_SearchMatch Input On entry: specifies whether to search for the first or last match. This also determines the pointer returned if an exact match cannot be found. Constraint: final ¼ Nag First or Nag Last. 8: match Pointer Output On exit: if an exact match is found this is a pointer to a pointer to the matching data object. If an exact match is not found this is set to point to the nearest object. If final ¼ Nag First this is the next later element, otherwise the next earlier element. 9: fail NagError Input/Output The NAG error argument (see Section 3.6 in the Essential Introduction). 6 Error Indicators and Warnings NE_BAD_PARAM On entry, argument final had an illegal value. On entry, argument order had an illegal value..2

NE_INT_ARG_EQ On entry, stride ¼ hvaluei. Constraint: stride ¼ 0. NE_INT_ARG_GT On entry, n ¼ hvaluei. Constraint: n hvaluei. On entry, stride ¼ hvaluei. Constraint: jstridej hvaluei. Theseargumentsarelimitedtoanimplementation-dependent size which is printed in the error message. NE_INT_ARG_LT On entry, n ¼ hvaluei. Constraint: n 0. 7 Accuracy Not applicable. 8 Parallelism and Performance Not applicable. 9 Further Comments The maximum time taken by the function is approximately proportional to log 2 n. 10 Example The example program reads a key and a list of real numbers, which have been sorted into ascending order. It then searches the list for the first number which matches the key. 10.1 Program Text / nag_search_vector () Example Program. Copyright 2014 Numerical Algorithms Group. Mark 2 revised, 1992. Mark 5 revised, 1998. Mark 7 revised, 2001. Mark 8 revised, 2004. / #include <nag.h> #include <stdio.h> #include <nag_stdlib.h> #include <nag_stddef.h> #include <nagm01.h> #ifdef cplusplus extern "C" static Integer NAG_CALL compare(const Nag_Pointer a, const Nag_Pointer b); #ifdef cplusplus.3

NAG Library Manual int main(void) Integer exit_status = 0; NagError fail; Pointer match; double key, vec = 0; size_t i, n; INIT_FAIL(fail); / Skip heading in data file / scanf_s("%[^\n]"); scanf("%[^\n]"); printf("nag_search_vector () Example Program Results\n"); / Read number of points and number to search for / scanf_s("%" NAG_UFMT "%lf", &n, &key); scanf("%" NAG_UFMT "%lf", &n, &key); if (n >= 1) if (!(vec = NAG_ALLOC(50, double))) printf("allocation failure\n"); exit_status = -1; goto END; printf("invalid n.\n"); exit_status = 1; return exit_status; for (i = 0; i < n; ++i) scanf_s("%lf", &vec[i]); scanf("%lf", &vec[i]); / nag_search_vector (). Searches a vector for either the first or last match to a given value / if (nag_search_vector((pointer) &key, (Pointer) vec, n, (ptrdiff_t)(sizeof(double)), compare, Nag_Ascending, Nag_First, &match, &fail)) printf("exact match found: "); if (fail.code!= NE_NOERROR) printf("error from nag_search_vector ().\n%s\n", fail.message); exit_status = 1; goto END; printf("first match index: %"NAG_UFMT"\n", (size_t)((double ) match - vec)); printf("no exact match found: "); if (match!= NULL) printf("nag_first nearest match index = %"NAG_UFMT"\n", (size_t)((double ) match - vec)); printf("no match in the input array\n");.4

END: NAG_FREE(vec); return exit_status; static Integer NAG_CALL compare(const Nag_Pointer a, const Nag_Pointer b) double x = ((const double ) a); double y = ((const double ) b); return(x < y?-1:(x == y?0:1)); 10.2 Program Data nag_search_vector () Example Program Data 20 2.3 0.5 0.5 1.1 1.2 1.2 1.2 1.3 2.1 2.3 2.3 2.3 2.3 4.1 5.8 5.9 6.3 6.5 6.5 8.6 9.9 10.3 Program Results nag_search_vector () Example Program Results Exact match found: First match index: 8.5 (last)