mirror of
https://github.com/lcn2/calc.git
synced 2025-08-16 01:03:29 +03:00
While help/errstr has been added, the errstr builtin function is not yet written. In anticipation of the new errstr builtin the rest of the calc error system has been updated to associated errsym E_STRING's with errnum error codes and errmsg error messages. Minor improvements to help/rand. The verify_error_table() function that does a verification the error_table[] array and setup private_error_alias[] array is now called by libcalc_call_me_first(). Fix comment about wrong include file in have_sys_mount.h. Removed unused booltostr() and strtobool() macros from bool.h. Moved define of math_error(char *, ...) from zmath.h to errtbl.h. The errtbl.h include file, unless ERRCODE_SRC is defined also includes attribute.h and errsym.h. Group calc error related builtin support functions together in func.c. Make switch indenting in func.c consistent. Passing an invalid argument to error(), errno() or strerror() will set errno AND throw a math error. Before errno would be set and an error value was returned. Before there was no way to tell if the error value was a result of the arg or if an error detected. Added E_STRING to error([errnum | "E_STRING"]) builtin function. Added E_STRING to errno([errnum | "E_STRING"]) builtin function. Added E_STRING to strerror([errnum | "E_STRING"]) builtin function. Calling these functions with an E_STRING errsym is the same as calling them with the matching errnum code. Standardized on calc computation error related E_STRING strings where there are a set of related codes. Changed "E_...digits" into "E_..._digits". For example, E_FPUTC1 became E_FPUTC_1, E_FPUTC2 became E_FPUTC_2, and E_FPUTC3 became E_FPUTC_3. In a few cases such as E_APPR became E_APPR_1, because there was a E_APPR2 (which became E_APPR_2) and E_APPR3 (which became E_APPR_3). To other special cases, E_ILOG10 became E_IBASE10_LOG and E_ILOG2 became E_IBASE2_LOG because E_ILOG10 and E_ILOG2 are both independent calc computation error related E_STRING strings. Now related sets of E_STRING strings end in _ (underscore) followed by digits. The following is the list of E_STRING strings changes: E_APPR ==> E_APPR_1 E_ROUND ==> E_ROUND_1 E_SQRT ==> E_SQRT_1 E_ROOT ==> E_ROOT_1 E_SHIFT ==> E_SHIFT_1 E_SCALE ==> E_SCALE_1 E_POWI ==> E_POWI_1 E_POWER ==> E_POWER_1 E_QUO ==> E_QUO_1 E_MOD ==> E_MOD_1 E_ABS ==> E_ABS_1 E_APPR2 ==> E_APPR_2 E_APPR3 ==> E_APPR_3 E_ROUND2 ==> E_ROUND_2 E_ROUND3 ==> E_ROUND_3 E_BROUND2 ==> E_BROUND_2 E_BROUND3 ==> E_BROUND_3 E_SQRT2 ==> E_SQRT_2 E_SQRT3 ==> E_SQRT_3 E_ROOT2 ==> E_ROOT_2 E_ROOT3 ==> E_ROOT_3 E_SHIFT2 ==> E_SHIFT_2 E_SCALE2 ==> E_SCALE_2 E_POWI2 ==> E_POWI_2 E_POWER2 ==> E_POWER_2 E_POWER3 ==> E_POWER_3 E_QUO2 ==> E_QUO_2 E_QUO3 ==> E_QUO_3 E_MOD2 ==> E_MOD_2 E_MOD3 ==> E_MOD_3 E_ABS2 ==> E_ABS_2 E_EXP1 ==> E_EXP_1 E_EXP2 ==> E_EXP_2 E_FPUTC1 ==> E_FPUTC_1 E_FPUTC2 ==> E_FPUTC_2 E_FPUTC3 ==> E_FPUTC_3 E_FGETC1 ==> E_FGETC_1 E_FGETC2 ==> E_FGETC_2 E_FOPEN1 ==> E_FOPEN_1 E_FOPEN2 ==> E_FOPEN_2 E_FREOPEN1 ==> E_FREOPEN_1 E_FREOPEN2 ==> E_FREOPEN_2 E_FREOPEN3 ==> E_FREOPEN_3 E_FCLOSE1 ==> E_FCLOSE_1 E_FPUTS1 ==> E_FPUTS_1 E_FPUTS2 ==> E_FPUTS_2 E_FPUTS3 ==> E_FPUTS_3 E_FGETS1 ==> E_FGETS_1 E_FGETS2 ==> E_FGETS_2 E_FPUTSTR1 ==> E_FPUTSTR_1 E_FPUTSTR2 ==> E_FPUTSTR_2 E_FPUTSTR3 ==> E_FPUTSTR_3 E_FGETSTR1 ==> E_FGETSTR_1 E_FGETSTR2 ==> E_FGETSTR_2 E_FGETLINE1 ==> E_FGETLINE_1 E_FGETLINE2 ==> E_FGETLINE_2 E_FGETFIELD1 ==> E_FGETFIELD_1 E_FGETFIELD2 ==> E_FGETFIELD_2 E_REWIND1 ==> E_REWIND_1 E_PRINTF1 ==> E_PRINTF_1 E_PRINTF2 ==> E_PRINTF_2 E_FPRINTF1 ==> E_FPRINTF_1 E_FPRINTF2 ==> E_FPRINTF_2 E_FPRINTF3 ==> E_FPRINTF_3 E_STRPRINTF1 ==> E_STRPRINTF_1 E_STRPRINTF2 ==> E_STRPRINTF_2 E_FSCAN1 ==> E_FSCAN_1 E_FSCAN2 ==> E_FSCAN_2 E_FSCANF1 ==> E_FSCANF_1 E_FSCANF2 ==> E_FSCANF_2 E_FSCANF3 ==> E_FSCANF_3 E_FSCANF4 ==> E_FSCANF_4 E_STRSCANF1 ==> E_STRSCANF_1 E_STRSCANF2 ==> E_STRSCANF_2 E_STRSCANF3 ==> E_STRSCANF_3 E_STRSCANF4 ==> E_STRSCANF_4 E_SCANF1 ==> E_SCANF_1 E_SCANF2 ==> E_SCANF_2 E_SCANF3 ==> E_SCANF_3 E_FTELL1 ==> E_FTELL_1 E_FTELL2 ==> E_FTELL_2 E_FSEEK1 ==> E_FSEEK_1 E_FSEEK2 ==> E_FSEEK_2 E_FSEEK3 ==> E_FSEEK_3 E_FSIZE1 ==> E_FSIZE_1 E_FSIZE2 ==> E_FSIZE_2 E_FEOF1 ==> E_FEOF_1 E_FEOF2 ==> E_FEOF_2 E_FERROR1 ==> E_FERROR_1 E_FERROR2 ==> E_FERROR_2 E_UNGETC1 ==> E_UNGETC_1 E_UNGETC2 ==> E_UNGETC_2 E_UNGETC3 ==> E_UNGETC_3 E_ISATTY1 ==> E_ISATTY_1 E_ISATTY2 ==> E_ISATTY_2 E_ACCESS1 ==> E_ACCESS_1 E_ACCESS2 ==> E_ACCESS_2 E_SEARCH1 ==> E_SEARCH_1 E_SEARCH2 ==> E_SEARCH_2 E_SEARCH3 ==> E_SEARCH_3 E_SEARCH4 ==> E_SEARCH_4 E_SEARCH5 ==> E_SEARCH_5 E_SEARCH6 ==> E_SEARCH_6 E_RSEARCH1 ==> E_RSEARCH_1 E_RSEARCH2 ==> E_RSEARCH_2 E_RSEARCH3 ==> E_RSEARCH_3 E_RSEARCH4 ==> E_RSEARCH_4 E_RSEARCH5 ==> E_RSEARCH_5 E_RSEARCH6 ==> E_RSEARCH_6 E_REWIND2 ==> E_REWIND_2 E_STRERROR1 ==> E_STRERROR_1 E_STRERROR2 ==> E_STRERROR_2 E_COS1 ==> E_COS_1 E_COS2 ==> E_COS_2 E_SIN1 ==> E_SIN_1 E_SIN2 ==> E_SIN_2 E_EVAL2 ==> E_EVAL_2 E_ARG1 ==> E_ARG_1 E_ARG2 ==> E_ARG_2 E_POLAR1 ==> E_POLAR_1 E_POLAR2 ==> E_POLAR_2 E_MATFILL1 ==> E_MATFILL_1 E_MATFILL2 ==> E_MATFILL_2 E_MATTRANS1 ==> E_MATTRANS_1 E_MATTRANS2 ==> E_MATTRANS_2 E_DET1 ==> E_DET_1 E_DET2 ==> E_DET_2 E_DET3 ==> E_DET_3 E_MATMIN1 ==> E_MATMIN_1 E_MATMIN2 ==> E_MATMIN_2 E_MATMIN3 ==> E_MATMIN_3 E_MATMAX1 ==> E_MATMAX_1 E_MATMAX2 ==> E_MATMAX_2 E_MATMAX3 ==> E_MATMAX_3 E_CP1 ==> E_CP_1 E_CP2 ==> E_CP_2 E_CP3 ==> E_CP_3 E_DP1 ==> E_DP_1 E_DP2 ==> E_DP_2 E_DP3 ==> E_DP_3 E_SUBSTR1 ==> E_SUBSTR_1 E_SUBSTR2 ==> E_SUBSTR_2 E_INSERT1 ==> E_INSERT_1 E_INSERT2 ==> E_INSERT_2 E_DELETE1 ==> E_DELETE_1 E_DELETE2 ==> E_DELETE_2 E_LN1 ==> E_LN_1 E_LN2 ==> E_LN_2 E_ERROR1 ==> E_ERROR_1 E_ERROR2 ==> E_ERROR_2 E_EVAL3 ==> E_EVAL_3 E_EVAL4 ==> E_EVAL_4 E_RM1 ==> E_RM_1 E_RM2 ==> E_RM_2 E_BLK1 ==> E_BLK_1 E_BLK2 ==> E_BLK_2 E_BLK3 ==> E_BLK_3 E_BLK4 ==> E_BLK_4 E_BLKFREE1 ==> E_BLKFREE_1 E_BLKFREE2 ==> E_BLKFREE_2 E_BLKFREE3 ==> E_BLKFREE_3 E_BLKFREE4 ==> E_BLKFREE_4 E_BLKFREE5 ==> E_BLKFREE_5 E_BLOCKS1 ==> E_BLOCKS_1 E_BLOCKS2 ==> E_BLOCKS_2 E_COPY1 ==> E_COPY_01 E_COPY2 ==> E_COPY_02 E_COPY3 ==> E_COPY_03 E_COPY4 ==> E_COPY_04 E_COPY5 ==> E_COPY_05 E_COPY6 ==> E_COPY_06 E_COPY7 ==> E_COPY_07 E_COPY8 ==> E_COPY_08 E_COPY9 ==> E_COPY_09 E_COPY10 ==> E_COPY_10 E_COPY11 ==> E_COPY_11 E_COPY12 ==> E_COPY_12 E_COPY13 ==> E_COPY_13 E_COPY14 ==> E_COPY_14 E_COPY15 ==> E_COPY_15 E_COPY16 ==> E_COPY_16 E_COPY17 ==> E_COPY_17 E_COPYF1 ==> E_COPYF_1 E_COPYF2 ==> E_COPYF_2 E_COPYF3 ==> E_COPYF_3 E_COPYF4 ==> E_COPYF_4 E_PROTECT1 ==> E_PROTECT_1 E_PROTECT2 ==> E_PROTECT_2 E_PROTECT3 ==> E_PROTECT_3 E_MATFILL3 ==> E_MATFILL_3 E_MATFILL4 ==> E_MATFILL_4 E_MATTRACE1 ==> E_MATTRACE_1 E_MATTRACE2 ==> E_MATTRACE_2 E_MATTRACE3 ==> E_MATTRACE_3 E_TAN1 ==> E_TAN_1 E_TAN2 ==> E_TAN_2 E_COT1 ==> E_COT_1 E_COT2 ==> E_COT_2 E_SEC1 ==> E_SEC_1 E_SEC2 ==> E_SEC_2 E_CSC1 ==> E_CSC_1 E_CSC2 ==> E_CSC_2 E_SINH1 ==> E_SINH_1 E_SINH2 ==> E_SINH_2 E_COSH1 ==> E_COSH_1 E_COSH2 ==> E_COSH_2 E_TANH1 ==> E_TANH_1 E_TANH2 ==> E_TANH_2 E_COTH1 ==> E_COTH_1 E_COTH2 ==> E_COTH_2 E_SECH1 ==> E_SECH_1 E_SECH2 ==> E_SECH_2 E_CSCH1 ==> E_CSCH_1 E_CSCH2 ==> E_CSCH_2 E_ASIN1 ==> E_ASIN_1 E_ASIN2 ==> E_ASIN_2 E_ACOS1 ==> E_ACOS_1 E_ACOS2 ==> E_ACOS_2 E_ATAN1 ==> E_ATAN_1 E_ATAN2 ==> E_ATAN_2 E_ACOT1 ==> E_ACOT_1 E_ACOT2 ==> E_ACOT_2 E_ASEC1 ==> E_ASEC_1 E_ASEC2 ==> E_ASEC_2 E_ACSC1 ==> E_ACSC_1 E_ACSC2 ==> E_ACSC_2 E_ASINH1 ==> E_ASINH_1 E_ASINH2 ==> E_ASINH_2 E_ACOSH1 ==> E_ACOSH_1 E_ACOSH2 ==> E_ACOSH_2 E_ATANH1 ==> E_ATANH_1 E_ATANH2 ==> E_ATANH_2 E_ACOTH1 ==> E_ACOTH_1 E_ACOTH2 ==> E_ACOTH_2 E_ASECH1 ==> E_ASECH_1 E_ASECH2 ==> E_ASECH_2 E_ACSCH1 ==> E_ACSCH_1 E_ACSCH2 ==> E_ACSCH_2 E_GD1 ==> E_GD_1 E_GD2 ==> E_GD_2 E_AGD1 ==> E_AGD_1 E_AGD2 ==> E_AGD_2 E_BIT1 ==> E_BIT_1 E_BIT2 ==> E_BIT_2 E_SETBIT1 ==> E_SETBIT_1 E_SETBIT2 ==> E_SETBIT_2 E_SETBIT3 ==> E_SETBIT_3 E_SEG1 ==> E_SEG_1 E_SEG2 ==> E_SEG_2 E_SEG3 ==> E_SEG_3 E_HIGHBIT1 ==> E_HIGHBIT_1 E_HIGHBIT2 ==> E_HIGHBIT_2 E_LOWBIT1 ==> E_LOWBIT_1 E_LOWBIT2 ==> E_LOWBIT_2 E_HEAD1 ==> E_HEAD_1 E_HEAD2 ==> E_HEAD_2 E_TAIL1 ==> E_TAIL_1 E_TAIL2 ==> E_TAIL_2 E_XOR1 ==> E_XOR_1 E_XOR2 ==> E_XOR_2 E_INDICES1 ==> E_INDICES_1 E_INDICES2 ==> E_INDICES_2 E_EXP3 ==> E_EXP_3 E_SINH3 ==> E_SINH_3 E_COSH3 ==> E_COSH_3 E_SIN3 ==> E_SIN_3 E_COS3 ==> E_COS_3 E_GD3 ==> E_GD_3 E_AGD3 ==> E_AGD_3 E_POWER4 ==> E_POWER_4 E_ROOT4 ==> E_ROOT_4 E_DGT1 ==> E_DGT_1 E_DGT2 ==> E_DGT_2 E_DGT3 ==> E_DGT_3 E_PLCS1 ==> E_PLCS_1 E_PLCS2 ==> E_PLCS_2 E_DGTS1 ==> E_DGTS_1 E_DGTS2 ==> E_DGTS_2 E_ILOG10 ==> E_IBASE10_LOG E_ILOG2 ==> E_IBASE2_LOG E_COMB1 ==> E_COMB_1 E_COMB2 ==> E_COMB_2 E_ASSIGN1 ==> E_ASSIGN_1 E_ASSIGN2 ==> E_ASSIGN_2 E_ASSIGN3 ==> E_ASSIGN_3 E_ASSIGN4 ==> E_ASSIGN_4 E_ASSIGN5 ==> E_ASSIGN_5 E_ASSIGN6 ==> E_ASSIGN_6 E_ASSIGN7 ==> E_ASSIGN_7 E_ASSIGN8 ==> E_ASSIGN_8 E_ASSIGN9 ==> E_ASSIGN_9 E_SWAP1 ==> E_SWAP_1 E_SWAP2 ==> E_SWAP_2 E_SWAP3 ==> E_SWAP_3 E_QUOMOD1 ==> E_QUOMOD_1 E_QUOMOD2 ==> E_QUOMOD_2 E_QUOMOD3 ==> E_QUOMOD_3 E_PREINC1 ==> E_PREINC_1 E_PREINC2 ==> E_PREINC_2 E_PREINC3 ==> E_PREINC_3 E_PREDEC1 ==> E_PREDEC_1 E_PREDEC2 ==> E_PREDEC_2 E_PREDEC3 ==> E_PREDEC_3 E_POSTINC1 ==> E_POSTINC_1 E_POSTINC2 ==> E_POSTINC_2 E_POSTINC3 ==> E_POSTINC_3 E_POSTDEC1 ==> E_POSTDEC_1 E_POSTDEC2 ==> E_POSTDEC_2 E_POSTDEC3 ==> E_POSTDEC_3 E_INIT1 ==> E_INIT_01 E_INIT2 ==> E_INIT_02 E_INIT3 ==> E_INIT_03 E_INIT4 ==> E_INIT_04 E_INIT5 ==> E_INIT_05 E_INIT6 ==> E_INIT_06 E_INIT7 ==> E_INIT_07 E_INIT8 ==> E_INIT_08 E_INIT9 ==> E_INIT_09 E_INIT10 ==> E_INIT_10 E_LIST1 ==> E_LIST_1 E_LIST2 ==> E_LIST_2 E_LIST3 ==> E_LIST_3 E_LIST4 ==> E_LIST_4 E_LIST5 ==> E_LIST_5 E_LIST6 ==> E_LIST_6 E_MODIFY1 ==> E_MODIFY_1 E_MODIFY2 ==> E_MODIFY_2 E_MODIFY3 ==> E_MODIFY_3 E_MODIFY4 ==> E_MODIFY_4 E_MODIFY5 ==> E_MODIFY_5 E_FPATHOPEN1 ==> E_FPATHOPEN_1 E_FPATHOPEN2 ==> E_FPATHOPEN_2 E_LOG1 ==> E_LOG_1 E_LOG2 ==> E_LOG_2 E_LOG3 ==> E_LOG_3 E_FGETFILE1 ==> E_FGETFILE_1 E_FGETFILE2 ==> E_FGETFILE_2 E_FGETFILE3 ==> E_FGETFILE_3 E_TAN3 ==> E_TAN_3 E_TAN4 ==> E_TAN_4 E_COT3 ==> E_COT_3 E_COT4 ==> E_COT_4 E_SEC3 ==> E_SEC_3 E_CSC3 ==> E_CSC_3 E_TANH3 ==> E_TANH_3 E_TANH4 ==> E_TANH_4 E_COTH3 ==> E_COTH_3 E_COTH4 ==> E_COTH_4 E_SECH3 ==> E_SECH_3 E_CSCH3 ==> E_CSCH_3 E_ASIN3 ==> E_ASIN_3 E_ACOS3 ==> E_ACOS_3 E_ASINH3 ==> E_ASINH_3 E_ACOSH3 ==> E_ACOSH_3 E_ATAN3 ==> E_ATAN_3 E_ACOT3 ==> E_ACOT_3 E_ASEC3 ==> E_ASEC_3 E_ACSC3 ==> E_ACSC_3 E_ATANH3 ==> E_ATANH_3 E_ACOTH3 ==> E_ACOTH_3 E_ASECH3 ==> E_ASECH_3 E_ACSCH3 ==> E_ACSCH_3 E_D2R1 ==> E_D2R_1 E_D2R2 ==> E_D2R_2 E_R2D1 ==> E_R2D_1 E_R2D2 ==> E_R2D_2 E_G2R1 ==> E_G2R_1 E_G2R2 ==> E_G2R_2 E_R2G1 ==> E_R2G_1 E_R2G2 ==> E_R2G_2 E_D2G1 ==> E_D2G_1 E_G2D1 ==> E_G2D_1 E_D2DMS1 ==> E_D2DMS_1 E_D2DMS2 ==> E_D2DMS_2 E_D2DMS3 ==> E_D2DMS_3 E_D2DMS4 ==> E_D2DMS_4 E_D2DM1 ==> E_D2DM_1 E_D2DM2 ==> E_D2DM_2 E_D2DM3 ==> E_D2DM_3 E_D2DM4 ==> E_D2DM_4 E_G2GMS1 ==> E_G2GMS_1 E_G2GMS2 ==> E_G2GMS_2 E_G2GMS3 ==> E_G2GMS_3 E_G2GMS4 ==> E_G2GMS_4 E_G2GM1 ==> E_G2GM_1 E_G2GM2 ==> E_G2GM_2 E_G2GM3 ==> E_G2GM_3 E_G2GM4 ==> E_G2GM_4 E_H2HMS1 ==> E_H2HMS_1 E_H2HMS2 ==> E_H2HMS_2 E_H2HMS3 ==> E_H2HMS_3 E_H2HMS4 ==> E_H2HMS_4 E_H2HM1 ==> E_H2HM_1 E_H2HM2 ==> E_H2HM_2 E_H2HM3 ==> E_H2HM_3 E_H2HM4 ==> E_H2HM_4 E_DMS2D1 ==> E_DMS2D_1 E_DMS2D2 ==> E_DMS2D_2 E_DM2D1 ==> E_DM2D_1 E_DM2D2 ==> E_DM2D_2 E_GMS2G1 ==> E_GMS2G_1 E_GMS2G2 ==> E_GMS2G_2 E_GM2G1 ==> E_GM2G_1 E_GM2G2 ==> E_GM2G_2 E_HMS2H1 ==> E_HMS2H_1 E_HMS2H2 ==> E_HMS2H_2 E_HM2H1 ==> E_HM2H_1 E_HM2H2 ==> E_HM2H_2 E_VERSIN1 ==> E_VERSIN_1 E_VERSIN2 ==> E_VERSIN_2 E_VERSIN3 ==> E_VERSIN_3 E_AVERSIN1 ==> E_AVERSIN_1 E_AVERSIN2 ==> E_AVERSIN_2 E_AVERSIN3 ==> E_AVERSIN_3 E_COVERSIN1 ==> E_COVERSIN_1 E_COVERSIN2 ==> E_COVERSIN_2 E_COVERSIN3 ==> E_COVERSIN_3 E_ACOVERSIN1 ==> E_ACOVERSIN_1 E_ACOVERSIN2 ==> E_ACOVERSIN_2 E_ACOVERSIN3 ==> E_ACOVERSIN_3 E_VERCOS1 ==> E_VERCOS_1 E_VERCOS2 ==> E_VERCOS_2 E_VERCOS3 ==> E_VERCOS_3 E_AVERCOS1 ==> E_AVERCOS_1 E_AVERCOS2 ==> E_AVERCOS_2 E_AVERCOS3 ==> E_AVERCOS_3 E_COVERCOS1 ==> E_COVERCOS_1 E_COVERCOS2 ==> E_COVERCOS_2 E_COVERCOS3 ==> E_COVERCOS_3 E_ACOVERCOS1 ==> E_ACOVERCOS_1 E_ACOVERCOS2 ==> E_ACOVERCOS_2 E_ACOVERCOS3 ==> E_ACOVERCOS_3 E_TAN5 ==> E_TAN_5 E_COT5 ==> E_COT_5 E_COT6 ==> E_COT_6 E_SEC5 ==> E_SEC_5 E_CSC5 ==> E_CSC_5 E_CSC6 ==> E_CSC_6
1821 lines
37 KiB
C
1821 lines
37 KiB
C
/*
|
|
* matfunc - extended precision rational arithmetic matrix functions
|
|
*
|
|
* Copyright (C) 1999-2007,2021-2023 David I. Bell
|
|
*
|
|
* Calc is open software; you can redistribute it and/or modify it under
|
|
* the terms of the version 2.1 of the GNU Lesser General Public License
|
|
* as published by the Free Software Foundation.
|
|
*
|
|
* Calc is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
|
|
* Public License for more details.
|
|
*
|
|
* A copy of version 2.1 of the GNU Lesser General Public License is
|
|
* distributed with calc under the filename COPYING-LGPL. You should have
|
|
* received a copy with calc; if not, write to Free Software Foundation, Inc.
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*
|
|
* Under source code control: 1990/02/15 01:48:18
|
|
* File existed as early as: before 1990
|
|
*
|
|
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
|
|
*/
|
|
|
|
/*
|
|
* Extended precision rational arithmetic matrix functions.
|
|
* Matrices can contain arbitrary types of elements.
|
|
*/
|
|
|
|
#include "alloc.h"
|
|
#include "value.h"
|
|
#include "zrand.h"
|
|
|
|
#include "have_unused.h"
|
|
|
|
|
|
#include "errtbl.h"
|
|
#include "banned.h" /* include after system header <> includes */
|
|
|
|
|
|
E_FUNC long irand(long s);
|
|
|
|
S_FUNC void matswaprow(MATRIX *m, long r1, long r2);
|
|
S_FUNC void matsubrow(MATRIX *m, long oprow, long baserow, VALUE *mulval);
|
|
S_FUNC void matmulrow(MATRIX *m, long row, VALUE *mulval);
|
|
S_FUNC MATRIX *matident(MATRIX *m);
|
|
|
|
|
|
|
|
/*
|
|
* Add two compatible matrices.
|
|
*/
|
|
MATRIX *
|
|
matadd(MATRIX *m1, MATRIX *m2)
|
|
{
|
|
int dim;
|
|
|
|
long min1, min2, max1, max2, index;
|
|
VALUE *v1, *v2, *vres;
|
|
MATRIX *res;
|
|
MATRIX tmp;
|
|
|
|
if (m1->m_dim != m2->m_dim) {
|
|
math_error("Incompatible matrix dimensions for add");
|
|
not_reached();
|
|
}
|
|
tmp.m_dim = m1->m_dim;
|
|
tmp.m_size = m1->m_size;
|
|
for (dim = 0; dim < m1->m_dim; dim++) {
|
|
min1 = m1->m_min[dim];
|
|
max1 = m1->m_max[dim];
|
|
min2 = m2->m_min[dim];
|
|
max2 = m2->m_max[dim];
|
|
if ((min1 && min2 && (min1 != min2)) ||
|
|
((max1-min1) != (max2-min2))) {
|
|
math_error("Incompatible matrix bounds for add");
|
|
not_reached();
|
|
}
|
|
tmp.m_min[dim] = (min1 ? min1 : min2);
|
|
tmp.m_max[dim] = tmp.m_min[dim] + (max1 - min1);
|
|
}
|
|
res = matalloc(m1->m_size);
|
|
*res = tmp;
|
|
v1 = m1->m_table;
|
|
v2 = m2->m_table;
|
|
vres = res->m_table;
|
|
for (index = m1->m_size; index > 0; index--)
|
|
addvalue(v1++, v2++, vres++);
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Subtract two compatible matrices.
|
|
*/
|
|
MATRIX *
|
|
matsub(MATRIX *m1, MATRIX *m2)
|
|
{
|
|
int dim;
|
|
long min1, min2, max1, max2, index;
|
|
VALUE *v1, *v2, *vres;
|
|
MATRIX *res;
|
|
MATRIX tmp;
|
|
|
|
if (m1->m_dim != m2->m_dim) {
|
|
math_error("Incompatible matrix dimensions for sub");
|
|
not_reached();
|
|
}
|
|
tmp.m_dim = m1->m_dim;
|
|
tmp.m_size = m1->m_size;
|
|
for (dim = 0; dim < m1->m_dim; dim++) {
|
|
min1 = m1->m_min[dim];
|
|
max1 = m1->m_max[dim];
|
|
min2 = m2->m_min[dim];
|
|
max2 = m2->m_max[dim];
|
|
if ((min1 && min2 && (min1 != min2)) ||
|
|
((max1-min1) != (max2-min2))) {
|
|
math_error("Incompatible matrix bounds for sub");
|
|
not_reached();
|
|
}
|
|
tmp.m_min[dim] = (min1 ? min1 : min2);
|
|
tmp.m_max[dim] = tmp.m_min[dim] + (max1 - min1);
|
|
}
|
|
res = matalloc(m1->m_size);
|
|
*res = tmp;
|
|
v1 = m1->m_table;
|
|
v2 = m2->m_table;
|
|
vres = res->m_table;
|
|
for (index = m1->m_size; index > 0; index--)
|
|
subvalue(v1++, v2++, vres++);
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Produce the negative of a matrix.
|
|
*/
|
|
MATRIX *
|
|
matneg(MATRIX *m)
|
|
{
|
|
register VALUE *val, *vres;
|
|
long index;
|
|
MATRIX *res;
|
|
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
val = m->m_table;
|
|
vres = res->m_table;
|
|
for (index = m->m_size; index > 0; index--)
|
|
negvalue(val++, vres++);
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Multiply two compatible matrices.
|
|
*/
|
|
MATRIX *
|
|
matmul(MATRIX *m1, MATRIX *m2)
|
|
{
|
|
register MATRIX *res;
|
|
long i1, i2, max1, max2, index, maxindex;
|
|
VALUE *v1, *v2, *vres;
|
|
VALUE sum, tmp1, tmp2;
|
|
|
|
if (m1->m_dim == 0) {
|
|
i2 = m2->m_size;
|
|
v2 = m2->m_table;
|
|
res = matalloc(i2);
|
|
*res = *m2;
|
|
vres = res->m_table;
|
|
while (i2-- > 0)
|
|
mulvalue(m1->m_table, v2++, vres++);
|
|
return res;
|
|
}
|
|
if (m2->m_dim == 0) {
|
|
i1 = m1->m_size;
|
|
v1 = m1->m_table;
|
|
res = matalloc(i1);
|
|
*res = *m1;
|
|
vres = res->m_table;
|
|
while (i1-- > 0)
|
|
mulvalue(v1++, m2->m_table, vres++);
|
|
return res;
|
|
}
|
|
if (m1->m_dim == 1 && m2->m_dim == 1) {
|
|
if (m1->m_max[0]-m1->m_min[0] != m2->m_max[0]-m2->m_min[0]) {
|
|
math_error("Incompatible bounds for 1D * 1D matmul");
|
|
not_reached();
|
|
}
|
|
res = matalloc(m1->m_size);
|
|
*res = *m1;
|
|
v1 = m1->m_table;
|
|
v2 = m2->m_table;
|
|
vres = res->m_table;
|
|
for (index = m1->m_size; index > 0; index--)
|
|
mulvalue(v1++, v2++, vres++);
|
|
return res;
|
|
}
|
|
if (m1->m_dim == 1 && m2->m_dim == 2) {
|
|
if (m1->m_max[0]-m1->m_min[0] != m2->m_max[0]-m2->m_min[0]) {
|
|
math_error("Incompatible bounds for 1D * 2D matmul");
|
|
not_reached();
|
|
}
|
|
res = matalloc(m2->m_size);
|
|
*res = *m2;
|
|
i1 = m1->m_max[0] - m1->m_min[0] + 1;
|
|
max2 = m2->m_max[1] - m2->m_min[1] + 1;
|
|
v1 = m1->m_table;
|
|
v2 = m2->m_table;
|
|
vres = res->m_table;
|
|
while (i1-- > 0) {
|
|
i2 = max2;
|
|
while (i2-- > 0)
|
|
mulvalue(v1, v2++, vres++);
|
|
v1++;
|
|
}
|
|
return res;
|
|
}
|
|
if (m1->m_dim == 2 && m2->m_dim == 1) {
|
|
if (m1->m_max[1]-m1->m_min[1] != m2->m_max[0]-m2->m_min[0]) {
|
|
math_error("Incompatible bounds for 2D * 1D matmul");
|
|
not_reached();
|
|
}
|
|
res = matalloc(m1->m_size);
|
|
*res = *m1;
|
|
i1 = m1->m_max[0] - m1->m_min[0] + 1;
|
|
max1 = m1->m_max[1] - m1->m_min[1] + 1;
|
|
v1 = m1->m_table;
|
|
vres = res->m_table;
|
|
while (i1-- > 0) {
|
|
v2 = m2->m_table;
|
|
i2 = max1;
|
|
while (i2-- > 0)
|
|
mulvalue(v1++, v2++, vres++);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
if ((m1->m_dim != 2) || (m2->m_dim != 2)) {
|
|
math_error("Matrix dimensions not compatible for mul");
|
|
not_reached();
|
|
}
|
|
if ((m1->m_max[1]-m1->m_min[1]) != (m2->m_max[0]-m2->m_min[0])) {
|
|
math_error("Incompatible bounds for 2D * 2D matrix mul");
|
|
not_reached();
|
|
}
|
|
max1 = (m1->m_max[0] - m1->m_min[0] + 1);
|
|
max2 = (m2->m_max[1] - m2->m_min[1] + 1);
|
|
maxindex = (m1->m_max[1] - m1->m_min[1] + 1);
|
|
res = matalloc(max1 * max2);
|
|
res->m_dim = 2;
|
|
res->m_min[0] = m1->m_min[0];
|
|
res->m_max[0] = m1->m_max[0];
|
|
res->m_min[1] = m2->m_min[1];
|
|
res->m_max[1] = m2->m_max[1];
|
|
for (i1 = 0; i1 < max1; i1++) {
|
|
for (i2 = 0; i2 < max2; i2++) {
|
|
sum.v_type = V_NULL;
|
|
sum.v_subtype = V_NOSUBTYPE;
|
|
v1 = &m1->m_table[i1 * maxindex];
|
|
v2 = &m2->m_table[i2];
|
|
for (index = 0; index < maxindex; index++) {
|
|
mulvalue(v1, v2, &tmp1);
|
|
addvalue(&sum, &tmp1, &tmp2);
|
|
freevalue(&tmp1);
|
|
freevalue(&sum);
|
|
sum = tmp2;
|
|
v1++;
|
|
if (index+1 < maxindex)
|
|
v2 += max2;
|
|
}
|
|
index = (i1 * max2) + i2;
|
|
res->m_table[index] = sum;
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Square a matrix.
|
|
*/
|
|
MATRIX *
|
|
matsquare(MATRIX *m)
|
|
{
|
|
register MATRIX *res;
|
|
long i1, i2, max, index;
|
|
VALUE *v1, *v2;
|
|
VALUE sum, tmp1, tmp2;
|
|
|
|
if (m->m_dim < 2) {
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
v1 = m->m_table;
|
|
v2 = res->m_table;
|
|
for (index = m->m_size; index > 0; index--)
|
|
squarevalue(v1++, v2++);
|
|
return res;
|
|
}
|
|
if (m->m_dim != 2) {
|
|
math_error("Matrix dimension exceeds two for square");
|
|
not_reached();
|
|
}
|
|
if ((m->m_max[0] - m->m_min[0]) != (m->m_max[1] - m->m_min[1])) {
|
|
math_error("Squaring non-square matrix");
|
|
not_reached();
|
|
}
|
|
max = (m->m_max[0] - m->m_min[0] + 1);
|
|
res = matalloc(max * max);
|
|
res->m_dim = 2;
|
|
res->m_min[0] = m->m_min[0];
|
|
res->m_max[0] = m->m_max[0];
|
|
res->m_min[1] = m->m_min[1];
|
|
res->m_max[1] = m->m_max[1];
|
|
for (i1 = 0; i1 < max; i1++) {
|
|
for (i2 = 0; i2 < max; i2++) {
|
|
sum.v_type = V_NULL;
|
|
sum.v_subtype = V_NOSUBTYPE;
|
|
v1 = &m->m_table[i1 * max];
|
|
v2 = &m->m_table[i2];
|
|
for (index = 0; index < max; index++) {
|
|
mulvalue(v1, v2, &tmp1);
|
|
addvalue(&sum, &tmp1, &tmp2);
|
|
freevalue(&tmp1);
|
|
freevalue(&sum);
|
|
sum = tmp2;
|
|
v1++;
|
|
v2 += max;
|
|
}
|
|
index = (i1 * max) + i2;
|
|
res->m_table[index] = sum;
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Compute the result of raising a matrix to an integer power if
|
|
* dimension <= 2 and for dimension == 2, the matrix is square.
|
|
* Negative powers mean the positive power of the inverse.
|
|
* Note: This calculation could someday be improved for large powers
|
|
* by using the characteristic polynomial of the matrix.
|
|
*
|
|
* given:
|
|
* m matrix to be raised
|
|
* q power to raise it to
|
|
*/
|
|
MATRIX *
|
|
matpowi(MATRIX *m, NUMBER *q)
|
|
{
|
|
MATRIX *res, *tmp;
|
|
long power; /* power to raise to */
|
|
FULL bit; /* current bit value */
|
|
|
|
if (m->m_dim > 2) {
|
|
math_error("Matrix dimension greater than 2 for power");
|
|
not_reached();
|
|
}
|
|
if (m->m_dim == 2 && (m->m_max[0] - m->m_min[0] !=
|
|
m->m_max[1] - m->m_min[1])) {
|
|
math_error("Raising non-square 2D matrix to a power");
|
|
not_reached();
|
|
}
|
|
if (qisfrac(q)) {
|
|
math_error("Raising matrix to non-integral power");
|
|
not_reached();
|
|
}
|
|
if (zge31b(q->num)) {
|
|
math_error("Raising matrix to very large power");
|
|
not_reached();
|
|
}
|
|
power = ztolong(q->num);
|
|
if (qisneg(q))
|
|
power = -power;
|
|
/*
|
|
* Handle some low powers specially
|
|
*/
|
|
if ((power <= 4) && (power >= -2)) {
|
|
switch ((int) power) {
|
|
case 0:
|
|
return matident(m);
|
|
case 1:
|
|
return matcopy(m);
|
|
case -1:
|
|
return matinv(m);
|
|
case 2:
|
|
return matsquare(m);
|
|
case -2:
|
|
tmp = matinv(m);
|
|
res = matsquare(tmp);
|
|
matfree(tmp);
|
|
return res;
|
|
case 3:
|
|
tmp = matsquare(m);
|
|
res = matmul(m, tmp);
|
|
matfree(tmp);
|
|
return res;
|
|
case 4:
|
|
tmp = matsquare(m);
|
|
res = matsquare(tmp);
|
|
matfree(tmp);
|
|
return res;
|
|
}
|
|
}
|
|
if (power < 0) {
|
|
m = matinv(m);
|
|
power = -power;
|
|
}
|
|
/*
|
|
* Compute the power by squaring and multiplying.
|
|
* This uses the left to right method of power raising.
|
|
*/
|
|
bit = TOPFULL;
|
|
while ((bit & power) == 0)
|
|
bit >>= 1L;
|
|
bit >>= 1L;
|
|
res = matsquare(m);
|
|
if (bit & power) {
|
|
tmp = matmul(res, m);
|
|
matfree(res);
|
|
res = tmp;
|
|
}
|
|
bit >>= 1L;
|
|
while (bit) {
|
|
tmp = matsquare(res);
|
|
matfree(res);
|
|
res = tmp;
|
|
if (bit & power) {
|
|
tmp = matmul(res, m);
|
|
matfree(res);
|
|
res = tmp;
|
|
}
|
|
bit >>= 1L;
|
|
}
|
|
if (qisneg(q))
|
|
matfree(m);
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Calculate the cross product of two one dimensional matrices each
|
|
* with three components.
|
|
* m3 = matcross(m1, m2);
|
|
*/
|
|
MATRIX *
|
|
matcross(MATRIX *m1, MATRIX *m2)
|
|
{
|
|
MATRIX *res;
|
|
VALUE *v1, *v2, *vr;
|
|
VALUE tmp1, tmp2;
|
|
|
|
res = matalloc(3L);
|
|
res->m_dim = 1;
|
|
res->m_min[0] = 0;
|
|
res->m_max[0] = 2;
|
|
v1 = m1->m_table;
|
|
v2 = m2->m_table;
|
|
vr = res->m_table;
|
|
mulvalue(v1 + 1, v2 + 2, &tmp1);
|
|
mulvalue(v1 + 2, v2 + 1, &tmp2);
|
|
subvalue(&tmp1, &tmp2, vr + 0);
|
|
freevalue(&tmp1);
|
|
freevalue(&tmp2);
|
|
mulvalue(v1 + 2, v2 + 0, &tmp1);
|
|
mulvalue(v1 + 0, v2 + 2, &tmp2);
|
|
subvalue(&tmp1, &tmp2, vr + 1);
|
|
freevalue(&tmp1);
|
|
freevalue(&tmp2);
|
|
mulvalue(v1 + 0, v2 + 1, &tmp1);
|
|
mulvalue(v1 + 1, v2 + 0, &tmp2);
|
|
subvalue(&tmp1, &tmp2, vr + 2);
|
|
freevalue(&tmp1);
|
|
freevalue(&tmp2);
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Return the dot product of two matrices.
|
|
* result = matdot(m1, m2);
|
|
*/
|
|
VALUE
|
|
matdot(MATRIX *m1, MATRIX *m2)
|
|
{
|
|
VALUE *v1, *v2;
|
|
VALUE result, tmp1, tmp2;
|
|
long len;
|
|
|
|
v1 = m1->m_table;
|
|
v2 = m2->m_table;
|
|
mulvalue(v1, v2, &result);
|
|
len = m1->m_size;
|
|
while (--len > 0) {
|
|
mulvalue(++v1, ++v2, &tmp1);
|
|
addvalue(&result, &tmp1, &tmp2);
|
|
freevalue(&tmp1);
|
|
freevalue(&result);
|
|
result = tmp2;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
/*
|
|
* Scale the elements of a matrix by a specified power of two.
|
|
*
|
|
* given:
|
|
* m matrix to be scaled
|
|
* n scale factor
|
|
*/
|
|
MATRIX *
|
|
matscale(MATRIX *m, long n)
|
|
{
|
|
register VALUE *val, *vres;
|
|
VALUE temp;
|
|
long index;
|
|
MATRIX *res; /* resulting matrix */
|
|
|
|
if (n == 0)
|
|
return matcopy(m);
|
|
temp.v_type = V_NUM;
|
|
temp.v_subtype = V_NOSUBTYPE;
|
|
temp.v_num = itoq(n);
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
val = m->m_table;
|
|
vres = res->m_table;
|
|
for (index = m->m_size; index > 0; index--)
|
|
scalevalue(val++, &temp, vres++);
|
|
qfree(temp.v_num);
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Shift the elements of a matrix by the specified number of bits.
|
|
* Positive shift means leftwards, negative shift rightwards.
|
|
*
|
|
* given:
|
|
* m matrix to be shifted
|
|
* n shift count
|
|
*/
|
|
MATRIX *
|
|
matshift(MATRIX *m, long n)
|
|
{
|
|
register VALUE *val, *vres;
|
|
VALUE temp;
|
|
long index;
|
|
MATRIX *res; /* resulting matrix */
|
|
|
|
if (n == 0)
|
|
return matcopy(m);
|
|
temp.v_type = V_NUM;
|
|
temp.v_subtype = V_NOSUBTYPE;
|
|
temp.v_num = itoq(n);
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
val = m->m_table;
|
|
vres = res->m_table;
|
|
for (index = m->m_size; index > 0; index--)
|
|
shiftvalue(val++, &temp, false, vres++);
|
|
qfree(temp.v_num);
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Multiply the elements of a matrix by a specified value.
|
|
*
|
|
* given:
|
|
* m matrix to be multiplied
|
|
* vp value to multiply by
|
|
*/
|
|
MATRIX *
|
|
matmulval(MATRIX *m, VALUE *vp)
|
|
{
|
|
register VALUE *val, *vres;
|
|
long index;
|
|
MATRIX *res;
|
|
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
val = m->m_table;
|
|
vres = res->m_table;
|
|
for (index = m->m_size; index > 0; index--)
|
|
mulvalue(val++, vp, vres++);
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Divide the elements of a matrix by a specified value, keeping
|
|
* only the integer quotient.
|
|
*
|
|
* given:
|
|
* m matrix to be divided
|
|
* vp value to divide by
|
|
* v3 rounding type parameter
|
|
*/
|
|
MATRIX *
|
|
matquoval(MATRIX *m, VALUE *vp, VALUE *v3)
|
|
{
|
|
register VALUE *val, *vres;
|
|
long index;
|
|
MATRIX *res;
|
|
|
|
if ((vp->v_type == V_NUM) && qiszero(vp->v_num)) {
|
|
math_error("Division by zero");
|
|
not_reached();
|
|
}
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
val = m->m_table;
|
|
vres = res->m_table;
|
|
for (index = m->m_size; index > 0; index--)
|
|
quovalue(val++, vp, v3, vres++);
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Divide the elements of a matrix by a specified value, keeping
|
|
* only the remainder of the division.
|
|
*
|
|
* given:
|
|
* m matrix to be divided
|
|
* vp value to divide by
|
|
* v3 rounding type parameter
|
|
*/
|
|
MATRIX *
|
|
matmodval(MATRIX *m, VALUE *vp, VALUE *v3)
|
|
{
|
|
register VALUE *val, *vres;
|
|
long index;
|
|
MATRIX *res;
|
|
|
|
if ((vp->v_type == V_NUM) && qiszero(vp->v_num)) {
|
|
math_error("Division by zero");
|
|
not_reached();
|
|
}
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
val = m->m_table;
|
|
vres = res->m_table;
|
|
for (index = m->m_size; index > 0; index--)
|
|
modvalue(val++, vp, v3, vres++);
|
|
return res;
|
|
}
|
|
|
|
|
|
VALUE
|
|
mattrace(MATRIX *m)
|
|
{
|
|
VALUE *vp;
|
|
VALUE sum;
|
|
VALUE tmp;
|
|
long i, j;
|
|
|
|
if (m->m_dim < 2) {
|
|
matsum(m, &sum);
|
|
return sum;
|
|
}
|
|
if (m->m_dim != 2)
|
|
return error_value(E_MATTRACE_2);
|
|
i = (m->m_max[0] - m->m_min[0] + 1);
|
|
j = (m->m_max[1] - m->m_min[1] + 1);
|
|
if (i != j)
|
|
return error_value(E_MATTRACE_3);
|
|
vp = m->m_table;
|
|
copyvalue(vp, &sum);
|
|
j++;
|
|
while (--i > 0) {
|
|
vp += j;
|
|
addvalue(&sum, vp, &tmp);
|
|
freevalue(&sum);
|
|
sum = tmp;
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
|
|
/*
|
|
* Transpose a 2-dimensional matrix
|
|
*/
|
|
MATRIX *
|
|
mattrans(MATRIX *m)
|
|
{
|
|
register VALUE *v1, *v2; /* current values */
|
|
long rows, cols; /* rows and columns in new matrix */
|
|
long row, col; /* current row and column */
|
|
MATRIX *res;
|
|
|
|
if (m->m_dim < 2)
|
|
return matcopy(m);
|
|
res = matalloc(m->m_size);
|
|
res->m_dim = 2;
|
|
res->m_min[0] = m->m_min[1];
|
|
res->m_max[0] = m->m_max[1];
|
|
res->m_min[1] = m->m_min[0];
|
|
res->m_max[1] = m->m_max[0];
|
|
rows = (m->m_max[1] - m->m_min[1] + 1);
|
|
cols = (m->m_max[0] - m->m_min[0] + 1);
|
|
v1 = res->m_table;
|
|
for (row = 0; row < rows; row++) {
|
|
v2 = &m->m_table[row];
|
|
for (col = 0; col < cols; col++) {
|
|
copyvalue(v2, v1);
|
|
v1++;
|
|
v2 += rows;
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Produce a matrix with values all of which are conjugated.
|
|
*/
|
|
MATRIX *
|
|
matconj(MATRIX *m)
|
|
{
|
|
register VALUE *val, *vres;
|
|
long index;
|
|
MATRIX *res;
|
|
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
val = m->m_table;
|
|
vres = res->m_table;
|
|
for (index = m->m_size; index > 0; index--)
|
|
conjvalue(val++, vres++);
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Round elements of a matrix to specified number of decimal digits
|
|
*/
|
|
MATRIX *
|
|
matround(MATRIX *m, VALUE *v2, VALUE *v3)
|
|
{
|
|
VALUE *p, *q;
|
|
long s;
|
|
MATRIX *res;
|
|
|
|
s = m->m_size;
|
|
res = matalloc(s);
|
|
*res = *m;
|
|
p = m->m_table;
|
|
q = res->m_table;
|
|
while (s-- > 0)
|
|
roundvalue(p++, v2, v3, q++);
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Round elements of a matrix to specified number of binary digits
|
|
*/
|
|
MATRIX *
|
|
matbround(MATRIX *m, VALUE *v2, VALUE *v3)
|
|
{
|
|
VALUE *p, *q;
|
|
long s;
|
|
MATRIX *res;
|
|
|
|
s = m->m_size;
|
|
res = matalloc(s);
|
|
*res = *m;
|
|
p = m->m_table;
|
|
q = res->m_table;
|
|
while (s-- > 0)
|
|
broundvalue(p++, v2, v3, q++);
|
|
return res;
|
|
}
|
|
|
|
/*
|
|
* Approximate a matrix by approximating elements to be multiples of
|
|
* v2, rounding type determined by v3.
|
|
*/
|
|
MATRIX *
|
|
matappr(MATRIX *m, VALUE *v2, VALUE *v3)
|
|
{
|
|
VALUE *p, *q;
|
|
long s;
|
|
MATRIX *res;
|
|
|
|
s = m->m_size;
|
|
res = matalloc(s);
|
|
*res = *m;
|
|
p = m->m_table;
|
|
q = res->m_table;
|
|
while (s-- > 0)
|
|
apprvalue(p++, v2, v3, q++);
|
|
return res;
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
* Produce a matrix with values all of which have been truncated to integers.
|
|
*/
|
|
MATRIX *
|
|
matint(MATRIX *m)
|
|
{
|
|
register VALUE *val, *vres;
|
|
long index;
|
|
MATRIX *res;
|
|
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
val = m->m_table;
|
|
vres = res->m_table;
|
|
for (index = m->m_size; index > 0; index--)
|
|
intvalue(val++, vres++);
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Produce a matrix with values all of which have only the fraction part left.
|
|
*/
|
|
MATRIX *
|
|
matfrac(MATRIX *m)
|
|
{
|
|
register VALUE *val, *vres;
|
|
long index;
|
|
MATRIX *res;
|
|
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
val = m->m_table;
|
|
vres = res->m_table;
|
|
for (index = m->m_size; index > 0; index--)
|
|
fracvalue(val++, vres++);
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Index a matrix normally by the specified set of index values.
|
|
* Returns the address of the matrix element if it is valid, or generates
|
|
* an error if the index values are out of range. The create flag is true
|
|
* if the element is to be written, but this is ignored here.
|
|
*
|
|
* given:
|
|
* mp matrix to operate on
|
|
* create true => create if element does not exist
|
|
* dim dimension of the indexing
|
|
* indices table of values being indexed by
|
|
*/
|
|
/*ARGSUSED*/
|
|
VALUE *
|
|
matindex(MATRIX *mp, bool UNUSED(create), long dim, VALUE *indices)
|
|
{
|
|
NUMBER *q; /* index value */
|
|
VALUE *vp;
|
|
long index; /* index value as an integer */
|
|
long offset; /* current offset into array */
|
|
int i; /* loop counter */
|
|
|
|
if (dim < 0) {
|
|
math_error("Negative dimension %ld for matrix", dim);
|
|
not_reached();
|
|
}
|
|
for (;;) {
|
|
if (dim < mp->m_dim) {
|
|
math_error(
|
|
"Indexing a %ldd matrix as a %ldd matrix",
|
|
mp->m_dim, dim);
|
|
not_reached();
|
|
}
|
|
offset = 0;
|
|
for (i = 0; i < mp->m_dim; i++) {
|
|
if (indices->v_type != V_NUM) {
|
|
math_error("Non-numeric index for matrix");
|
|
not_reached();
|
|
}
|
|
q = indices->v_num;
|
|
if (qisfrac(q)) {
|
|
math_error("Non-integral index for matrix");
|
|
not_reached();
|
|
}
|
|
index = qtoi(q);
|
|
if (zge31b(q->num) || (index < mp->m_min[i]) ||
|
|
(index > mp->m_max[i])) {
|
|
math_error("Index out of bounds for matrix");
|
|
not_reached();
|
|
}
|
|
offset *= (mp->m_max[i] - mp->m_min[i] + 1);
|
|
offset += (index - mp->m_min[i]);
|
|
indices++;
|
|
}
|
|
vp = mp->m_table + offset;
|
|
dim -= mp->m_dim;
|
|
if (dim == 0)
|
|
break;
|
|
if (vp->v_type != V_MAT) {
|
|
math_error("Non-matrix argument for matindex");
|
|
not_reached();
|
|
}
|
|
mp = vp->v_mat;
|
|
}
|
|
return vp;
|
|
}
|
|
|
|
|
|
/*
|
|
* Returns the list of indices for a matrix element with specified
|
|
* double-bracket index.
|
|
*/
|
|
LIST *
|
|
matindices(MATRIX *mp, long index)
|
|
{
|
|
LIST *lp;
|
|
int j;
|
|
long d;
|
|
VALUE val;
|
|
|
|
if (index < 0 || index >= mp->m_size)
|
|
return NULL;
|
|
|
|
lp = listalloc();
|
|
val.v_type = V_NUM;
|
|
val.v_subtype = V_NOSUBTYPE;
|
|
j = mp->m_dim;
|
|
|
|
while (--j >= 0) {
|
|
d = mp->m_max[j] - mp->m_min[j] + 1;
|
|
val.v_num = itoq(index % d + mp->m_min[j]);
|
|
insertlistfirst(lp, &val);
|
|
qfree(val.v_num);
|
|
index /= d;
|
|
}
|
|
return lp;
|
|
}
|
|
|
|
|
|
/*
|
|
* Search a matrix for the specified value, starting with the specified index.
|
|
* Returns 0 and stores index if value found; otherwise returns 1.
|
|
*/
|
|
int
|
|
matsearch(MATRIX *m, VALUE *vp, long i, long j, ZVALUE *index)
|
|
{
|
|
register VALUE *val;
|
|
|
|
val = &m->m_table[i];
|
|
if (i < 0 || j > m->m_size) {
|
|
math_error("This should not happen in call to matsearch");
|
|
not_reached();
|
|
}
|
|
while (i < j) {
|
|
if (acceptvalue(val++, vp)) {
|
|
utoz(i, index);
|
|
return 0;
|
|
}
|
|
i++;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
|
|
/*
|
|
* Search a matrix backwards for the specified value, starting with the
|
|
* specified index. Returns 0 and stores index if value found; otherwise
|
|
* returns 1.
|
|
*/
|
|
int
|
|
matrsearch(MATRIX *m, VALUE *vp, long i, long j, ZVALUE *index)
|
|
{
|
|
register VALUE *val;
|
|
|
|
if (i < 0 || j > m->m_size) {
|
|
math_error("This should not happen in call to matrsearch");
|
|
not_reached();
|
|
}
|
|
val = &m->m_table[--j];
|
|
while (j >= i) {
|
|
if (acceptvalue(val--, vp)) {
|
|
utoz(j, index);
|
|
return 0;
|
|
}
|
|
j--;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
/*
|
|
* Fill all of the elements of a matrix with one of two specified values.
|
|
* All entries are filled with the first specified value, except that if
|
|
* the matrix is w-dimensional and the second value pointer is non-NULL, then
|
|
* all diagonal entries are filled with the second value. This routine
|
|
* affects the supplied matrix directly, and doesn't return a copy.
|
|
*
|
|
* given:
|
|
* m matrix to be filled
|
|
* v1 value to fill most of matrix with
|
|
* v2 value for diagonal entries or null
|
|
*/
|
|
void
|
|
matfill(MATRIX *m, VALUE *v1, VALUE *v2)
|
|
{
|
|
register VALUE *val;
|
|
VALUE temp1, temp2;
|
|
long rows, cols;
|
|
long i, j;
|
|
|
|
copyvalue(v1, &temp1);
|
|
|
|
val = m->m_table;
|
|
if (m->m_dim != 2 || v2 == NULL) {
|
|
for (i = m->m_size; i > 0; i--) {
|
|
freevalue(val);
|
|
copyvalue(&temp1, val++);
|
|
}
|
|
freevalue(&temp1);
|
|
return;
|
|
}
|
|
|
|
copyvalue(v2, &temp2);
|
|
rows = m->m_max[0] - m->m_min[0] + 1;
|
|
cols = m->m_max[1] - m->m_min[1] + 1;
|
|
|
|
for (i = 0; i < rows; i++) {
|
|
for (j = 0; j < cols; j++) {
|
|
freevalue(val);
|
|
if (i == j)
|
|
copyvalue(&temp2, val++);
|
|
else
|
|
copyvalue(&temp1, val++);
|
|
}
|
|
}
|
|
freevalue(&temp1);
|
|
freevalue(&temp2);
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
* Set a copy of a square matrix to the identity matrix.
|
|
*/
|
|
S_FUNC MATRIX *
|
|
matident(MATRIX *m)
|
|
{
|
|
register VALUE *val; /* current value */
|
|
long row, col; /* current row and column */
|
|
long rows; /* number of rows */
|
|
MATRIX *res; /* resulting matrix */
|
|
|
|
if (m->m_dim != 2) {
|
|
math_error(
|
|
"Matrix dimension must be two for setting to identity");
|
|
not_reached();
|
|
}
|
|
if ((m->m_max[0] - m->m_min[0]) != (m->m_max[1] - m->m_min[1])) {
|
|
math_error("Matrix must be square for setting to identity");
|
|
not_reached();
|
|
}
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
val = res->m_table;
|
|
rows = (res->m_max[0] - res->m_min[0] + 1);
|
|
for (row = 0; row < rows; row++) {
|
|
for (col = 0; col < rows; col++) {
|
|
val->v_type = V_NUM;
|
|
val->v_subtype = V_NOSUBTYPE;
|
|
val->v_num = ((row == col) ? qlink(&_qone_) :
|
|
qlink(&_qzero_));
|
|
val++;
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Calculate the inverse of a matrix if it exists.
|
|
* This is done by using transformations on the supplied matrix to convert
|
|
* it to the identity matrix, and simultaneously applying the same set of
|
|
* transformations to the identity matrix.
|
|
*/
|
|
MATRIX *
|
|
matinv(MATRIX *m)
|
|
{
|
|
MATRIX *res; /* matrix to become the inverse */
|
|
long rows; /* number of rows */
|
|
long cur; /* current row being worked on */
|
|
long row, col; /* temp row and column values */
|
|
VALUE *val; /* current value in matrix*/
|
|
VALUE *vres; /* current value in result for dim < 2 */
|
|
VALUE mulval; /* value to multiply rows by */
|
|
VALUE tmpval; /* temporary value */
|
|
|
|
if (m->m_dim < 2) {
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
val = m->m_table;
|
|
vres = res->m_table;
|
|
for (cur = m->m_size; cur > 0; cur--)
|
|
invertvalue(val++, vres++);
|
|
return res;
|
|
}
|
|
if (m->m_dim != 2) {
|
|
math_error("Matrix dimension exceeds two for inverse");
|
|
not_reached();
|
|
}
|
|
if ((m->m_max[0] - m->m_min[0]) != (m->m_max[1] - m->m_min[1])) {
|
|
math_error("Inverting non-square matrix");
|
|
not_reached();
|
|
}
|
|
/*
|
|
* Begin by creating the identity matrix with the same attributes.
|
|
*/
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
rows = (m->m_max[0] - m->m_min[0] + 1);
|
|
val = res->m_table;
|
|
for (row = 0; row < rows; row++) {
|
|
for (col = 0; col < rows; col++) {
|
|
if (row == col)
|
|
val->v_num = qlink(&_qone_);
|
|
else
|
|
val->v_num = qlink(&_qzero_);
|
|
val->v_type = V_NUM;
|
|
val->v_subtype = V_NOSUBTYPE;
|
|
val++;
|
|
}
|
|
}
|
|
/*
|
|
* Now loop over each row, and eliminate all entries in the
|
|
* corresponding column by using row operations. Do the same
|
|
* operations on the resulting matrix. Copy the original matrix
|
|
* so that we don't destroy it.
|
|
*/
|
|
m = matcopy(m);
|
|
for (cur = 0; cur < rows; cur++) {
|
|
/*
|
|
* Find the first nonzero value in the rest of the column
|
|
* downwards from [cur,cur]. If there is no such value, then
|
|
* the matrix is not invertible. If the first nonzero entry
|
|
* is not the current row, then swap the two rows to make the
|
|
* current one nonzero.
|
|
*/
|
|
row = cur;
|
|
val = &m->m_table[(row * rows) + row];
|
|
while (testvalue(val) == 0) {
|
|
if (++row >= rows) {
|
|
matfree(m);
|
|
matfree(res);
|
|
math_error("Matrix is not invertible");
|
|
not_reached();
|
|
}
|
|
val += rows;
|
|
}
|
|
invertvalue(val, &mulval);
|
|
if (row != cur) {
|
|
matswaprow(m, row, cur);
|
|
matswaprow(res, row, cur);
|
|
}
|
|
/*
|
|
* Now for every other nonzero entry in the current column,
|
|
* subtract the appropriate multiple of the current row to
|
|
* force that entry to become zero.
|
|
*/
|
|
val = &m->m_table[cur];
|
|
for (row = 0; row < rows; row++) {
|
|
if ((row == cur) || (testvalue(val) == 0)) {
|
|
if (row+1 < rows)
|
|
val += rows;
|
|
continue;
|
|
}
|
|
mulvalue(val, &mulval, &tmpval);
|
|
matsubrow(m, row, cur, &tmpval);
|
|
matsubrow(res, row, cur, &tmpval);
|
|
freevalue(&tmpval);
|
|
if (row+1 < rows)
|
|
val += rows;
|
|
}
|
|
freevalue(&mulval);
|
|
}
|
|
/*
|
|
* Now the original matrix has nonzero entries only on its main
|
|
* diagonal. Scale the rows of the result matrix by the inverse
|
|
* of those entries.
|
|
*/
|
|
val = m->m_table;
|
|
for (row = 0; row < rows; row++) {
|
|
if ((val->v_type != V_NUM) || !qisone(val->v_num)) {
|
|
invertvalue(val, &mulval);
|
|
matmulrow(res, row, &mulval);
|
|
freevalue(&mulval);
|
|
}
|
|
if (row+1 < rows)
|
|
val += (rows + 1);
|
|
}
|
|
matfree(m);
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Calculate the determinant of a square matrix.
|
|
* This uses the fraction-free Gauss-Bareiss algorithm.
|
|
*/
|
|
VALUE
|
|
matdet(MATRIX *m)
|
|
{
|
|
long n; /* original matrix is n x n */
|
|
long k; /* working sub-matrix is k x k */
|
|
long i, j;
|
|
VALUE *pivot, *div, *val;
|
|
VALUE *vp, *vv;
|
|
VALUE tmp1, tmp2, tmp3;
|
|
bool neg; /* whether to negate determinant */
|
|
|
|
if (m->m_dim < 2) {
|
|
vp = m->m_table;
|
|
i = m->m_size;
|
|
copyvalue(vp, &tmp1);
|
|
|
|
while (--i > 0) {
|
|
mulvalue(&tmp1, ++vp, &tmp2);
|
|
freevalue(&tmp1);
|
|
tmp1 = tmp2;
|
|
}
|
|
return tmp1;
|
|
}
|
|
|
|
if (m->m_dim != 2)
|
|
return error_value(E_DET_2);
|
|
if ((m->m_max[0] - m->m_min[0]) != (m->m_max[1] - m->m_min[1]))
|
|
return error_value(E_DET_3);
|
|
|
|
/*
|
|
* Loop over each row, and eliminate all lower entries in the
|
|
* corresponding column by using row operations. Copy the original
|
|
* matrix so that we don't destroy it.
|
|
*/
|
|
neg = false;
|
|
m = matcopy(m);
|
|
n = (m->m_max[0] - m->m_min[0] + 1);
|
|
pivot = div = m->m_table;
|
|
for (k = n; k > 0; k--) {
|
|
/*
|
|
* Find the first nonzero value in the rest of the column
|
|
* downwards from pivot. If there is no such value, then
|
|
* the determinant is zero. If the first nonzero entry is not
|
|
* the pivot, then swap rows in the k * k sub-matrix, and
|
|
* remember that the determinant changes sign.
|
|
*/
|
|
val = pivot;
|
|
i = k;
|
|
while (!testvalue(val)) {
|
|
if (--i <= 0) {
|
|
tmp1.v_type = V_NUM;
|
|
tmp1.v_subtype = V_NOSUBTYPE;
|
|
tmp1.v_num = qlink(&_qzero_);
|
|
matfree(m);
|
|
return tmp1;
|
|
}
|
|
val += n;
|
|
}
|
|
if (i < k) {
|
|
vp = pivot;
|
|
vv = val;
|
|
j = k;
|
|
while (j-- > 0) {
|
|
tmp1 = *vp;
|
|
*vp++ = *vv;
|
|
*vv++ = tmp1;
|
|
}
|
|
neg = !neg;
|
|
}
|
|
/*
|
|
* Now for every val below the pivot, for each entry to
|
|
* the right of val, calculate the 2 x 2 determinant
|
|
* with corners at the pivot and the entry. If
|
|
* k < n, divide by div (the previous pivot value).
|
|
*/
|
|
val = pivot;
|
|
i = k;
|
|
while (--i > 0) {
|
|
val += n;
|
|
vp = pivot;
|
|
vv = val;
|
|
j = k;
|
|
while (--j > 0) {
|
|
mulvalue(pivot, ++vv, &tmp1);
|
|
mulvalue(val, ++vp, &tmp2);
|
|
subvalue(&tmp1, &tmp2, &tmp3);
|
|
freevalue(&tmp1);
|
|
freevalue(&tmp2);
|
|
freevalue(vv);
|
|
if (k < n) {
|
|
divvalue(&tmp3, div, vv);
|
|
freevalue(&tmp3);
|
|
}
|
|
else
|
|
*vv = tmp3;
|
|
}
|
|
}
|
|
div = pivot;
|
|
if (k > 0)
|
|
pivot += n + 1;
|
|
}
|
|
if (neg)
|
|
negvalue(div, &tmp1);
|
|
else
|
|
copyvalue(div, &tmp1);
|
|
matfree(m);
|
|
return tmp1;
|
|
}
|
|
|
|
|
|
/*
|
|
* Local utility routine to swap two rows of a square matrix.
|
|
* No checks are made to verify the legality of the arguments.
|
|
*/
|
|
S_FUNC void
|
|
matswaprow(MATRIX *m, long r1, long r2)
|
|
{
|
|
register VALUE *v1, *v2;
|
|
register long rows;
|
|
VALUE tmp;
|
|
|
|
if (r1 == r2)
|
|
return;
|
|
rows = (m->m_max[0] - m->m_min[0] + 1);
|
|
v1 = &m->m_table[r1 * rows];
|
|
v2 = &m->m_table[r2 * rows];
|
|
while (rows-- > 0) {
|
|
tmp = *v1;
|
|
*v1 = *v2;
|
|
*v2 = tmp;
|
|
v1++;
|
|
v2++;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* Local utility routine to subtract a multiple of one row to another one.
|
|
* The row to be changed is oprow, the row to be subtracted is baserow.
|
|
* No checks are made to verify the legality of the arguments.
|
|
*/
|
|
S_FUNC void
|
|
matsubrow(MATRIX *m, long oprow, long baserow, VALUE *mulval)
|
|
{
|
|
register VALUE *vop, *vbase;
|
|
register long entries;
|
|
VALUE tmp1, tmp2;
|
|
|
|
entries = (m->m_max[0] - m->m_min[0] + 1);
|
|
vop = &m->m_table[oprow * entries];
|
|
vbase = &m->m_table[baserow * entries];
|
|
while (entries-- > 0) {
|
|
mulvalue(vbase, mulval, &tmp1);
|
|
subvalue(vop, &tmp1, &tmp2);
|
|
freevalue(&tmp1);
|
|
freevalue(vop);
|
|
*vop = tmp2;
|
|
vop++;
|
|
vbase++;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* Local utility routine to multiply a row by a specified number.
|
|
* No checks are made to verify the legality of the arguments.
|
|
*/
|
|
S_FUNC void
|
|
matmulrow(MATRIX *m, long row, VALUE *mulval)
|
|
{
|
|
register VALUE *val;
|
|
register long rows;
|
|
VALUE tmp;
|
|
|
|
rows = (m->m_max[0] - m->m_min[0] + 1);
|
|
val = &m->m_table[row * rows];
|
|
while (rows-- > 0) {
|
|
mulvalue(val, mulval, &tmp);
|
|
freevalue(val);
|
|
*val = tmp;
|
|
val++;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* Make a full copy of a matrix.
|
|
*/
|
|
MATRIX *
|
|
matcopy(MATRIX *m)
|
|
{
|
|
MATRIX *res;
|
|
register VALUE *v1, *v2;
|
|
register long i;
|
|
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
v1 = m->m_table;
|
|
v2 = res->m_table;
|
|
i = m->m_size;
|
|
while (i-- > 0) {
|
|
copyvalue(v1, v2);
|
|
v1++;
|
|
v2++;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Make a matrix the same size as another and filled with a fixed value.
|
|
*
|
|
* given:
|
|
* m matrix to initialize
|
|
* v1 value to fill most of matrix with
|
|
* v2 value for diagonal entries (or NULL)
|
|
*/
|
|
MATRIX *
|
|
matinit(MATRIX *m, VALUE *v1, VALUE *v2)
|
|
{
|
|
MATRIX *res;
|
|
register VALUE *v;
|
|
register long i;
|
|
long row;
|
|
long rows;
|
|
|
|
/*
|
|
* clone matrix size
|
|
*/
|
|
res = matalloc(m->m_size);
|
|
*res = *m;
|
|
|
|
/*
|
|
* firewall
|
|
*/
|
|
if (v2 && ((res->m_dim != 2) ||
|
|
((res->m_max[0] - res->m_min[0]) !=
|
|
(res->m_max[1] - res->m_min[1])))) {
|
|
math_error("Filling diagonals of non-square matrix");
|
|
not_reached();
|
|
}
|
|
|
|
/*
|
|
* fill the bulk of the matrix
|
|
*/
|
|
v = res->m_table;
|
|
if (v2 == NULL) {
|
|
i = m->m_size;
|
|
while (i-- > 0) {
|
|
copyvalue(v1, v++);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
/*
|
|
* fill the diagonal of a square matrix if requested
|
|
*/
|
|
rows = res->m_max[0] - res->m_min[0] + 1;
|
|
v = res->m_table;
|
|
for (row = 0; row < rows; row++) {
|
|
copyvalue(v2, v+row);
|
|
v += rows;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* Allocate a matrix with the specified number of elements.
|
|
*/
|
|
MATRIX *
|
|
matalloc(long size)
|
|
{
|
|
MATRIX *m;
|
|
long i;
|
|
VALUE *vp;
|
|
|
|
m = (MATRIX *) malloc(matsize(size));
|
|
if (m == NULL) {
|
|
math_error("Cannot get memory to allocate matrix of size %ld",
|
|
size);
|
|
not_reached();
|
|
}
|
|
m->m_size = size;
|
|
for (i = size, vp = m->m_table; i > 0; i--, vp++)
|
|
vp->v_subtype = V_NOSUBTYPE;
|
|
return m;
|
|
}
|
|
|
|
|
|
/*
|
|
* Free a matrix, along with all of its element values.
|
|
*/
|
|
void
|
|
matfree(MATRIX *m)
|
|
{
|
|
register VALUE *vp;
|
|
register long i;
|
|
|
|
vp = m->m_table;
|
|
i = m->m_size;
|
|
while (i-- > 0)
|
|
freevalue(vp++);
|
|
free(m);
|
|
}
|
|
|
|
|
|
/*
|
|
* Test whether a matrix has any "nonzero" values.
|
|
* Returns true if so.
|
|
*/
|
|
bool
|
|
mattest(MATRIX *m)
|
|
{
|
|
register VALUE *vp;
|
|
register long i;
|
|
|
|
vp = m->m_table;
|
|
i = m->m_size;
|
|
while (i-- > 0) {
|
|
if (testvalue(vp++))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/*
|
|
* Sum the elements in a matrix.
|
|
*/
|
|
void
|
|
matsum(MATRIX *m, VALUE *vres)
|
|
{
|
|
VALUE *vp;
|
|
VALUE tmp; /* first sum value */
|
|
VALUE sum; /* final sum value */
|
|
long i;
|
|
|
|
vp = m->m_table;
|
|
i = m->m_size;
|
|
copyvalue(vp, &sum);
|
|
|
|
while (--i > 0) {
|
|
addvalue(&sum, ++vp, &tmp);
|
|
freevalue(&sum);
|
|
sum = tmp;
|
|
}
|
|
*vres = sum;
|
|
}
|
|
|
|
|
|
/*
|
|
* Test whether or not two matrices are equal.
|
|
* Equality is determined by the shape and values of the matrices,
|
|
* but not by their index bounds. Returns true if they differ.
|
|
*/
|
|
bool
|
|
matcmp(MATRIX *m1, MATRIX *m2)
|
|
{
|
|
VALUE *v1, *v2;
|
|
long i;
|
|
|
|
if (m1 == m2)
|
|
return false;
|
|
if ((m1->m_dim != m2->m_dim) || (m1->m_size != m2->m_size))
|
|
return true;
|
|
for (i = 0; i < m1->m_dim; i++) {
|
|
if ((m1->m_max[i] - m1->m_min[i]) !=
|
|
(m2->m_max[i] - m2->m_min[i]))
|
|
return true;
|
|
}
|
|
v1 = m1->m_table;
|
|
v2 = m2->m_table;
|
|
i = m1->m_size;
|
|
while (i-- > 0) {
|
|
if (comparevalue(v1++, v2++))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
void
|
|
matreverse(MATRIX *m)
|
|
{
|
|
VALUE *p, *q;
|
|
VALUE tmp;
|
|
|
|
p = m->m_table;
|
|
q = m->m_table + m->m_size - 1;
|
|
while (q > p) {
|
|
tmp = *p;
|
|
*p++ = *q;
|
|
*q-- = tmp;
|
|
}
|
|
}
|
|
|
|
|
|
void
|
|
matsort(MATRIX *m)
|
|
{
|
|
VALUE *a, *b, *next, *end;
|
|
VALUE *buf, *p;
|
|
VALUE *S[LONG_BITS];
|
|
long len[LONG_BITS];
|
|
long i, j, k;
|
|
|
|
buf = (VALUE *) malloc(m->m_size * sizeof(VALUE));
|
|
if (buf == NULL) {
|
|
math_error("Not enough memory for matsort");
|
|
not_reached();
|
|
}
|
|
next = m->m_table;
|
|
end = next + m->m_size;
|
|
for (k = 0; next && k < LONG_BITS; k++) {
|
|
S[k] = next++; /* S[k] is start of a run */
|
|
len[k] = 1;
|
|
if (next == end)
|
|
next = NULL;
|
|
while (k > 0 && (!next || len[k] >= len[k - 1])) {/* merging */
|
|
j = len[k];
|
|
b = S[k--];
|
|
i = len[k];
|
|
a = S[k];
|
|
len[k] += j;
|
|
p = buf;
|
|
if (precvalue(b, a)) {
|
|
do {
|
|
*p++ = *b++;
|
|
j--;
|
|
} while (j > 0 && precvalue(b,a));
|
|
if (j == 0) {
|
|
memcpy(p, a, i * sizeof(VALUE));
|
|
memcpy(S[k], buf,
|
|
len[k] * sizeof(VALUE));
|
|
continue;
|
|
}
|
|
}
|
|
|
|
do {
|
|
do {
|
|
*p++ = *a++;
|
|
i--;
|
|
} while (i > 0 && !precvalue(b,a));
|
|
if (i == 0) {
|
|
break;
|
|
}
|
|
do {
|
|
*p++ = *b++;
|
|
j--;
|
|
} while (j > 0 && precvalue(b,a));
|
|
} while (j != 0);
|
|
|
|
if (i == 0) {
|
|
memcpy(S[k], buf, (p - buf) * sizeof(VALUE));
|
|
} else if (j == 0) {
|
|
memcpy(p, a, i * sizeof(VALUE));
|
|
memcpy(S[k], buf, len[k] * sizeof(VALUE));
|
|
}
|
|
}
|
|
}
|
|
free(buf);
|
|
if (k >= LONG_BITS) {
|
|
/* this should never happen */
|
|
math_error("impossible k overflow in matsort!");
|
|
not_reached();
|
|
}
|
|
}
|
|
|
|
void
|
|
matrandperm(MATRIX *m)
|
|
{
|
|
VALUE *vp;
|
|
long s, i;
|
|
VALUE val;
|
|
|
|
s = m->m_size;
|
|
for (vp = m->m_table; s > 1; vp++, s--) {
|
|
i = irand(s);
|
|
if (i > 0) {
|
|
val = *vp;
|
|
*vp = vp[i];
|
|
vp[i] = val;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* Test whether or not a matrix is the identity matrix.
|
|
* Returns true if so.
|
|
*/
|
|
bool
|
|
matisident(MATRIX *m)
|
|
{
|
|
register VALUE *val; /* current value */
|
|
long row, col; /* row and column numbers */
|
|
|
|
val = m->m_table;
|
|
if (m->m_dim == 0) {
|
|
return (val->v_type == V_NUM && qisone(val->v_num));
|
|
}
|
|
if (m->m_dim == 1) {
|
|
for (row = m->m_min[0]; row <= m->m_max[0]; row++, val++) {
|
|
if (val->v_type != V_NUM || !qisone(val->v_num))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
if ((m->m_dim != 2) ||
|
|
((m->m_max[0] - m->m_min[0]) != (m->m_max[1] - m->m_min[1])))
|
|
return false;
|
|
for (row = m->m_min[0]; row <= m->m_max[0]; row++) {
|
|
/*
|
|
* We could use col = m->m_min[1]; col < m->m_max[1]
|
|
* but if m->m_min[0] != m->m_min[1] this won't work.
|
|
* We know that we have a square 2-dimensional matrix
|
|
* so we will pretend that m->m_min[0] == m->m_min[1].
|
|
*/
|
|
for (col = m->m_min[0]; col <= m->m_max[0]; col++) {
|
|
if (val->v_type != V_NUM)
|
|
return false;
|
|
if (row == col) {
|
|
if (!qisone(val->v_num))
|
|
return false;
|
|
} else {
|
|
if (!qiszero(val->v_num))
|
|
return false;
|
|
}
|
|
val++;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
/*
|
|
* Print a matrix and possibly few of its elements.
|
|
* The argument supplied specifies how many elements to allow printing.
|
|
* If any elements are printed, they are printed in short form.
|
|
*/
|
|
void
|
|
matprint(MATRIX *m, long max_print)
|
|
{
|
|
VALUE *vp;
|
|
long fullsize, count, index;
|
|
long dim, i, j;
|
|
char *msg;
|
|
long sizes[MAXDIM];
|
|
|
|
dim = m->m_dim;
|
|
fullsize = 1;
|
|
for (i = dim - 1; i >= 0; i--) {
|
|
sizes[i] = fullsize;
|
|
fullsize *= (m->m_max[i] - m->m_min[i] + 1);
|
|
}
|
|
msg = ((max_print > 0) ? "\nmat [" : "mat [");
|
|
if (dim) {
|
|
for (i = 0; i < dim; i++) {
|
|
if (m->m_min[i]) {
|
|
math_fmt("%s%ld:%ld", msg,
|
|
m->m_min[i], m->m_max[i]);
|
|
} else {
|
|
math_fmt("%s%ld", msg, m->m_max[i] + 1);
|
|
}
|
|
msg = ",";
|
|
}
|
|
} else {
|
|
math_str("mat [");
|
|
}
|
|
if (max_print > fullsize) {
|
|
max_print = fullsize;
|
|
}
|
|
vp = m->m_table;
|
|
count = 0;
|
|
for (index = 0; index < fullsize; index++) {
|
|
if ((vp->v_type != V_NUM) || !qiszero(vp->v_num))
|
|
count++;
|
|
vp++;
|
|
}
|
|
math_fmt("] (%ld element%s, %ld nonzero)",
|
|
fullsize, (fullsize == 1) ? "" : "s", count);
|
|
if (max_print <= 0)
|
|
return;
|
|
|
|
/*
|
|
* Now print the first few elements of the matrix in short
|
|
* and unambiguous format.
|
|
*/
|
|
math_str(":\n");
|
|
vp = m->m_table;
|
|
for (index = 0; index < max_print; index++) {
|
|
msg = " [";
|
|
j = index;
|
|
if (dim) {
|
|
for (i = 0; i < dim; i++) {
|
|
math_fmt("%s%ld", msg,
|
|
m->m_min[i] + (j / sizes[i]));
|
|
j %= sizes[i];
|
|
msg = ",";
|
|
}
|
|
} else {
|
|
math_str(msg);
|
|
}
|
|
math_str("] = ");
|
|
printvalue(vp++, PRINT_SHORT | PRINT_UNAMBIG);
|
|
math_str("\n");
|
|
}
|
|
if (max_print < fullsize)
|
|
math_str(" ...\n");
|
|
}
|