Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow constructing RIF and CIF from its string representation? #36797

Open
1 task done
user202729 opened this issue Dec 2, 2023 · 0 comments
Open
1 task done

Allow constructing RIF and CIF from its string representation? #36797

user202729 opened this issue Dec 2, 2023 · 0 comments

Comments

@user202729
Copy link
Contributor

user202729 commented Dec 2, 2023

Problem Description

As in the title. It would be nice if we can do the following and make it work

CIF('-1.184458063318075? - 0.9684329629159336?*I')
RIF('-2.564230760621332?')

since that's how the elements are printed by default.

Proposed Solution

I think it wouldn't be too hard to implement it, since RIF and CIF constructor already accept strings:

RIF('-2.564230760621332')
CIF('-1.184458063318075', '-0.9684329629159336')

Alternatives Considered

Additional Information

Quick implementation with regex:

import re
from typing import Union
def repr_to_RIF_or_CIF(s: str)->Union[RIF, CIF]:
	match = re.fullmatch(r'(-?\d+\.?\d*)\?(e-?\d+)?(?:\s*([+-]?\s*\d+\.?\d*)\?(e-?\d+)?\*I)?', s)
	if not match: raise TypeError(f'unable to convert {s!r} to real or complex interval')
	if match[3]:
		return CIF(match[1]+(match[2] or ''), match[3]+(match[4] or ''))
	else:
		return RIF(match[1]+(match[2] or ''))

Is there an existing issue for this?

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.
vbraun pushed a commit to vbraun/sage that referenced this issue Dec 6, 2024
    
As in the title. It allows you to say e.g. `RIF("1.23?2e-5")`.

Partially handles sagemath#36797. (only
for real case. Complex case is not handled yet, but in principle it
should not be too difficult.)

(I don't see any disadvantage of allowing this, it's backwards
compatible)

Issue: currently

```
sage: RIF("10", base=37)
37
sage: ZZ("10", base=37)
[error]
```

should this inconsistency be fixed? If so how?

[Edit: actually the rule of conversion should probably follow [string to
mpfr conversion rule](https://www.mpfr.org/mpfr-current/mpfr.html#index-
mpfr_005fstrtofr) or [string to mpz conversion
rule](https://gmplib.org/manual/Assigning-Integers) ]

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview. (there's no documentation change, but should we explicitly
mention the feature? I think the feature to construct from `[a..b]`
isn't explicitly mentioned either…?)

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->


sagemath#39001
    
URL: sagemath#38998
Reported by: user202729
Reviewer(s): Travis Scrimshaw
vbraun pushed a commit to vbraun/sage that referenced this issue Dec 8, 2024
    
As in the title. It allows you to say e.g. `RIF("1.23?2e-5")`.

Partially handles sagemath#36797. (only
for real case. Complex case is not handled yet, but in principle it
should not be too difficult.)

(I don't see any disadvantage of allowing this, it's backwards
compatible)

Issue: currently

```
sage: RIF("10", base=37)
37
sage: ZZ("10", base=37)
[error]
```

should this inconsistency be fixed? If so how?

[Edit: actually the rule of conversion should probably follow [string to
mpfr conversion rule](https://www.mpfr.org/mpfr-current/mpfr.html#index-
mpfr_005fstrtofr) or [string to mpz conversion
rule](https://gmplib.org/manual/Assigning-Integers) ]

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview. (there's no documentation change, but should we explicitly
mention the feature? I think the feature to construct from `[a..b]`
isn't explicitly mentioned either…?)

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->


sagemath#39001
    
URL: sagemath#38998
Reported by: user202729
Reviewer(s): Travis Scrimshaw
vbraun pushed a commit to vbraun/sage that referenced this issue Dec 11, 2024
    
As in the title. It allows you to say e.g. `RIF("1.23?2e-5")`.

Partially handles sagemath#36797. (only
for real case. Complex case is not handled yet, but in principle it
should not be too difficult.)

(I don't see any disadvantage of allowing this, it's backwards
compatible)

Issue: currently

```
sage: RIF("10", base=37)
37
sage: ZZ("10", base=37)
[error]
```

should this inconsistency be fixed? If so how?

[Edit: actually the rule of conversion should probably follow [string to
mpfr conversion rule](https://www.mpfr.org/mpfr-current/mpfr.html#index-
mpfr_005fstrtofr) or [string to mpz conversion
rule](https://gmplib.org/manual/Assigning-Integers) ]

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview. (there's no documentation change, but should we explicitly
mention the feature? I think the feature to construct from `[a..b]`
isn't explicitly mentioned either…?)

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->


sagemath#39001
    
URL: sagemath#38998
Reported by: user202729
Reviewer(s): Travis Scrimshaw
vbraun pushed a commit to vbraun/sage that referenced this issue Dec 12, 2024
    
As in the title. It allows you to say e.g. `RIF("1.23?2e-5")`.

Partially handles sagemath#36797. (only
for real case. Complex case is not handled yet, but in principle it
should not be too difficult.)

(I don't see any disadvantage of allowing this, it's backwards
compatible)

Issue: currently

```
sage: RIF("10", base=37)
37
sage: ZZ("10", base=37)
[error]
```

should this inconsistency be fixed? If so how?

[Edit: actually the rule of conversion should probably follow [string to
mpfr conversion rule](https://www.mpfr.org/mpfr-current/mpfr.html#index-
mpfr_005fstrtofr) or [string to mpz conversion
rule](https://gmplib.org/manual/Assigning-Integers) ]

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview. (there's no documentation change, but should we explicitly
mention the feature? I think the feature to construct from `[a..b]`
isn't explicitly mentioned either…?)

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->


sagemath#39001
    
URL: sagemath#38998
Reported by: user202729
Reviewer(s): Travis Scrimshaw
vbraun pushed a commit to vbraun/sage that referenced this issue Dec 13, 2024
    
As in the title. It allows you to say e.g. `RIF("1.23?2e-5")`.

Partially handles sagemath#36797. (only
for real case. Complex case is not handled yet, but in principle it
should not be too difficult.)

(I don't see any disadvantage of allowing this, it's backwards
compatible)

Issue: currently

```
sage: RIF("10", base=37)
37
sage: ZZ("10", base=37)
[error]
```

should this inconsistency be fixed? If so how?

[Edit: actually the rule of conversion should probably follow [string to
mpfr conversion rule](https://www.mpfr.org/mpfr-current/mpfr.html#index-
mpfr_005fstrtofr) or [string to mpz conversion
rule](https://gmplib.org/manual/Assigning-Integers) ]

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview. (there's no documentation change, but should we explicitly
mention the feature? I think the feature to construct from `[a..b]`
isn't explicitly mentioned either…?)

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->


sagemath#39001
    
URL: sagemath#38998
Reported by: user202729
Reviewer(s): Travis Scrimshaw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant