Datasets:
Download code/common.py from ahmed-wez/hidden-capability-math: direct link, hf CLI and curl.
- Browser
- Download file 2.39 kB
-
https://huggingface.co/datasets/ahmed-wez/hidden-capability-math/resolve/main/code/common.py
- Command line
-
hf download hf://datasets/ahmed-wez/hidden-capability-math/code/common.py
-
curl -L -o common.py https://huggingface.co/datasets/ahmed-wez/hidden-capability-math/resolve/main/code/common.py
2.39 kB
| """Exact, deterministic primitives; no inference services or paid dependencies.""" | |
| import hashlib, json, math, random, re | |
| from fractions import Fraction | |
| import sympy as S | |
| VERSION = '0.1.0-candidate' | |
| SEED = 20261126 | |
| DOMAINS = ['algebra','functions','number_theory','sequences','probability','combinatorics','geometry','ratios_percentages','multi_step_word'] | |
| REGISTRY = {} | |
| def canonical(x): | |
| if isinstance(x, (tuple,list)): return '['+','.join(canonical(v) for v in x)+']' | |
| if isinstance(x, str): x=S.sympify(x) | |
| return str(S.cancel(x)) | |
| def digest(x): return hashlib.sha256(json.dumps(x,sort_keys=True,separators=(',',':')).encode()).hexdigest() | |
| def exact(expr, scope): | |
| return S.sympify(expr, locals={**{k:S.sympify(v) for k,v in scope.items()},'C':S.binomial,'fac':S.factorial,'gcd':S.gcd,'lcm':S.lcm,'floor':S.floor,'ceil':S.ceiling,'sqrt':S.sqrt,'abs':S.Abs}).doit() | |
| def register(domain, name, clauses, targets, sample, steps, *, concepts=2, depth=2, level='medium', family=None): | |
| tid=domain+'.'+name | |
| assert tid not in REGISTRY | |
| REGISTRY[tid]=dict(template_id=tid,domain=domain,structure_family=domain+'.'+(family or name),clauses=clauses,targets=targets,sample=sample,steps=steps,concepts=concepts,depth=depth,level=level) | |
| def make(tid, seed): | |
| t=REGISTRY[tid]; p=t['sample'](random.Random(seed)); scope=dict(p); states=[] | |
| for name, expr in t['steps']: | |
| v=exact(expr,scope); scope[name]=v | |
| states.append({'name':name,'expression':expr,'value':canonical(v)}) | |
| ans=states[-1]['value'] | |
| return {'domain':t['domain'],'template_id':tid,'structure_family':t['structure_family'],'parameters':p,'answer':ans,'answer_type':'integer' if S.sympify(ans).is_Integer else 'rational' if S.sympify(ans).is_Rational else 'exact_expression','intermediate_steps':states,'generation_seed':seed,'generator_version':VERSION,'difficulty_metadata':{'label':t['level'],'score':t['depth']+t['concepts']+sum(abs(v).bit_length() for v in p.values())/max(1,len(p))/8,'dependency_depth':t['depth'],'concept_count':t['concepts'],'step_count':len(states),'status':'estimated structural difficulty; not model-measured'}} | |
| def load_registry(): | |
| import generators.algebra, generators.functions, generators.number_theory, generators.sequences, generators.probability, generators.combinatorics, generators.geometry, generators.ratios_percentages, generators.multi_step_word | |