dongnunai3125 2015-11-17 14:18
浏览 43
已采纳

使用php在MySQL数据库中保存不同大小的双数组

I am trying to store the coefficients of a rational function into a MySQL 5.5 database. This is done using PHP 5, which I am rather new to.

In more detail I have two arrays each representing the coefficients of the numerator and denominator polynomial (factored form). The size of both arrays is dynamic between 1 and 10 and if needed I can sort the numbers into any arbitrary order. All numbers are of type double.

Stored in the Database I only have to compare the functions for equality. Which means I only need to figure out if both arrays have the same coefficients (order does not matter). E.g. [1,2,3] and [3,1,2] are the same.

As MySQL does not offer to save arrays directly, I am not sure how to approach this task in the best way.

My current idea, I am yet unsure if feasible, is to sort both arrays in descending order. Then typecast each double to a string of 8 symbols, concatenating all strings. Afterwards I would store that (long) string as VARCHAR. To compare two functions I would compare the numerator and denominator string and if both match, the functions are the same.

A problem I see with that approach is the 'typecasting'. Interpreting a double as 8 characters is pretty crude and I have no idea if the resulting symbols might end the string early or cause any trouble.

Which brings my to my question, does this way work or is there a better way to do it?

  • 写回答

1条回答 默认 最新

  • dqwh1205 2015-11-17 14:58
    关注

    Yes, that should work, and is probably a good way to do it.

    If you're only checking for equality, you just need to convert to some canonical string representation, and save that in the database. A shorter representation is better for space usage, but making it still human-readable would be nice as well, since that should help with ad-hoc queries and debugging.

    Note that order probably does matter for a rational function. Also, if you're dealing with floating-point, you need to watch out for numbers that aren't exactly equal but might be close enough for what you're doing.

    A few options:

    • Store two columns, each a comma-delimited list of numbers (with zeroes for missing coefficients in the middle). Put them together with implode.
    • Store one column, containing two comma-delimited lists separates by a / character.
    • Store two VARBINARY columns, each 8 × n bytes long (where n is the number of coefficients). This would be easy in C or Java or perl, in PHP use the pack function.
    • Store one VARBINARY column, format length byte + data + data. Once again, user the pack function.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值